• unity, 人物与移动跳台的交互


    ----更新(2015-7-1):

    也可以用itween来移动跳台。

    ----

    例如人物跳到往复运动的移动跳台上后应随跳台运动。

    要实现这个效果有两个思路,一是用运动学一是用动力学。

    用动力学实现就是通过设置人物collider和跳台collider摩擦属性(使用自已创建的physic material替换collider中原来的material,然后设置摩擦属性)。这个方法我没试出来,下面只说使用运动学方法实现:

    1,创建一个box作为移动跳台,用unity里内置的Animation Curves调个左右往复运动的动画。

    2,为移动跳台添加Rigidbody并勾选Is Kinematic。这样,移动跳台本身就不再接受动力学作用了,但是它会对其它物体产生动力学作用。(While the Rigidbody is marked isKinematic, it will not be affected by collisions, forces, or any other part of physX. This means that you will have to control the object by manipulating the Transform component directly. Kinematic Rigidbodies will affect other objects, but they themselves will not be affected by physics. 引自:http://docs.unity3d.com/Manual/class-Rigidbody.html

    3,在playerController脚本中实现逻辑,如果人物处于非跳起状态且向下的raycast获得的hitInfo。满足hitInfo.transform.gameObject.layer==LayerMask.NameToLayer ("movingPlatform"),即player落在移动平台上,则将player的Rigidbody的水平速度设为移动跳台的水平速度。而如果不是这种情况,即player落在普通静态地面上,则player的Rigidbody水平速度设为0。

    但需要注意的是移动跳台的速度并不是hitInfo.transform.gameObject.getComponent<Rigidbody>().velocity,实际上这个速度是0,因为移动跳台是用unity自带动画编辑功能搞的,而不是用物理搞的,所以要获得移动跳台的速度,需要如下做:

    为移动跳台添加计算速度的脚本:

    using UnityEngine;
    using System.Collections;
    
    public class calculateMovingPlatformVelocity : MonoBehaviour {
    	private Vector3 m_pos;
    	private Vector3 m_posF;
    	public Vector3 m_velocity;
    	
    	// Use this for initialization
    	void Start () {
    	}
    	
    	void FixedUpdate(){
    		////Debug.Log (gameObject.GetComponent<Rigidbody>().velocity);//the velocity is zero!!! so we cant use it
    		m_posF = m_pos;
    		m_pos = gameObject.transform.position;
    		m_velocity = (m_pos - m_posF) / Time.fixedDeltaTime;
    
    	}
    }

    在playerController脚本中获得此速度:hitInfo.transform.gameObject.GetComponent<calculateMovingPlatformVelocity>().m_velocity。

    另外要将player的body、foot以及movingPlatform的摩擦属性设为0和Minimum。

    4,至此差不多搞定了,唯一一个问题就是发现人物跳上移动跳台后跳台的运动会变得不那么流畅(略微卡顿),而且可以看出并不是性能问题导致的。后来我尝试对跳台的Animator作如下设置:Animator->Update Mode由Normal改为Animate Physics,卡顿就不再发生了,至此达到了理想效果。

    注:Update Mode取Animate Physics的含义:http://answers.unity3d.com/questions/587115/how-can-i-use-animate-physics-in-a-project.html

     

  • 相关阅读:
    mysql主从复制搭建
    centos下安装mysql
    Elasticsearch5.1.1+ik分词器+HEAD插件安装小记
    cento下安装elasticsearch2.4.2小记
    plotly线上绘图和离线绘图的区别
    利用ZotFile对Zotero中的文献进行整理
    数据0-1标准化
    指针和引用的区别
    C++中使用sstream进行类型转换(数字字符串转数字、数字转数字字符串)
    C++ 中字符串查找、字符串截取、字符串替换
  • 原文地址:https://www.cnblogs.com/wantnon/p/4457879.html
Copyright © 2020-2023  润新知