zoukankan      html  css  js  c++  java
  • 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

     

  • 相关阅读:
    js回调函数
    axios如何先请求A接口然后在请求B接口
    蓝桥杯省赛 区间移位(二分+玄学贪心)
    P1403 [AHOI2005]约数研究(筛法)
    P1029 最大公约数和最小公倍数问题(数论水题)
    洛谷P1147连续自然数和(前缀和)
    洛谷P1017进制转换(进制转换/取模)
    洛谷P1088火星人(stl/全排列)
    Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) D. Navigation System(最短路)
    Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) C. Remove Adjacent(贪心+暴力)
  • 原文地址:https://www.cnblogs.com/wantnon/p/4457879.html
Copyright © 2011-2022 走看看