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

     

  • 相关阅读:
    n个元素进栈,有几种出栈方式
    The following IP can be used to access Google website
    一个未排序整数数组,有正负数,重新排列使负数排在正数前面,并且要求不改变原来的 相对顺序 比如: input: 1,7,-5,9,-12,15 ans: -5,-12,1,7,9,15 要求时间复杂度O(N),空间O(1) 。
    当今世界最受人们重视的十大经典算法
    指针
    变量作用域和生存期
    一篇文章搞清spark内存管理
    Spark的Shuffle是怎么回事
    一篇文章搞清spark任务如何执行
    scala这写的都是啥?一篇文章搞清柯里化
  • 原文地址:https://www.cnblogs.com/wantnon/p/4457879.html
Copyright © 2011-2022 走看看