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

unity, 人物与移动跳台的交互的更多相关文章
- Unity NGUI 网络斗地主 -发牌 脚本交互
Unity NGUI 网络斗地主 -发牌 脚本交互 @By 灰太龙 Unity4.2.1f4 NGUI 3.0.4 本篇说的问题是脚本与控件的交互! 现在对界面进行了改进,先看副图! 1.制作发牌效果 ...
- [Unity][安卓]Unity和Android Studio 3.0 交互通讯(1)Android Studio 3.0 设置
[安卓]Android Studio 3.0 JDK安卓环境配置(2017.10) http://blog.csdn.net/bulademian/article/details/78387052 [ ...
- iOS端给unity发送消息,实现两者交互。
上一篇我们简单说了一下unity发消息给iOS端.现在我们就来说一下iOS端给unity发送消息的简单使用. 首先iOS端做得事情其实很简单就一句话,直接上代码 /** * 第一个参数:是unity那 ...
- Unity 3D与Android Studio安卓交互之-导出jar包
u3d与安卓 jar 包交互 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享 ...
- Unity 人物跟谁手指的移动(第一种方式)
长夜漫漫无心睡眠,敲敲代码,越敲越来劲! 我发现好多小朋友都在玩熊出没之xxxx这个游戏,居然打了一下午都没玩通第2关,我把测试也叫来陪我一起玩! 结果他也打不通,我再去叫策划,他也没打过,我去叫主管 ...
- unity 实现调用Windows窗口/对话框交互
Unity调用Window窗口 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...
- Unity中的 原生插件/平台交互 原理
http://blog.csdn.net/u010019717/article/details/78451660 声明: 内容摘录自: http://gad.qq.com/article/deta ...
- Unity 与 Android (Android Studio)的交互
http://blog.csdn.net/kuerjinjin/article/details/50177633 1.大体思路: 在Android Studio 中编译导出Jar库,提供函数供 Uni ...
- Unity UnityWebRequest实现与后端的交互
一般我们与后端对接的时候会用到UnityWebRequest这里简单使用这个与后端进行交互这个是总类 using UnityEngine;using System.Collections;using ...
随机推荐
- 关于ASP.NET MVC中Form Authentication与Windows Authentication的简单理解
一般互联网应用,如人人网,微博,都是需要用户登录的,如果用户不登陆,就不能使用此网站.所以,这里都是用FormAuthentication,要求用户填写用户名与密码,然后登录成功后,FormAuthe ...
- ubuntu Server 安装 php5
ubuntu Server 安装 php5 1:如果你的服务器已经安装了apache2组件,那么在安装php5时,可以把对应apache2的php5组件一起安装 sudo apt-get instal ...
- uni-app 如何开启sass\less处理
开启方式:工具->插件安装->安装完成,启用即可
- 如何理解VB窗体中的scale类属性及width height属性之间的关系
如何理解VB窗体中的scale类属性及width height属性之间的关系 VB中的SCALEHIEGT,SCALEWIDTH,与窗体中的WIDTH,HEIGHT的区别及关系是许多VB初学者难以理解 ...
- Python 图形界面(GUI)设计
不要问我为什么要用 Python 来做这种事,我回到“高兴咋地”也不是不可以,总之好奇有没有好的解决方案.逛了一圈下来,总体上来说,Python 图形界面有以下几个可行度比较高的解决方案. 1. py ...
- vue 渲染流程
1.DOM 节点树 高效的更新所有这些节点会是比较困难的,因为原生的DOM节点属性很多,渲染性能差. 2.虚拟 DOM “虚拟 DOM”是我们对由 Vue 组件树建立起来的整个 VNode 树的称呼. ...
- table设置表格有滚动条
table 设置表格有滚动条. 少说多做,代码中有注释: <!DOCTYPE HTML> <html> <head> <meta http-equiv=&qu ...
- exception PLS-00403: expression 'V_END' cannot be used as an INTO-target of a SELECT/FETCH statement
exception PLS-00403: expression 'V_END' cannot be used as an INTO-target of a SELECT/FETCH stateme ...
- eval、exec、execfile
# -*- coding: utf-8 -*- #python 27 #xiaodeng #http://blog.csdn.net/azhao_dn/article/details/6921654 ...
- Apache POI – Reading and Writing Excel file in Java
来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...