移动:

  1、SimpleMove(Vector3: vector3&speed)

    简单移动,可以根据vector3方向移动,物体不需要添加刚体即受重力影响,不需要添加碰撞器即可以产生碰撞,但无法推动其它物体。

  2、Move(Vector3: vector3&speed)

    移动,根据vector3方向移动,速度比SimpleMove快许多,不受重力影响,但可以在不添加碰撞器的情况下产生碰撞,无法推动其它物体。

sample

using UnityEngine;
using System.Collections; public class CharControl : MonoBehaviour { CharacterController charCtl ;
// Use this for initialization
void Start () {
charCtl = GetComponent<CharacterController>() ;
} // Update is called once per frame
void Update () {
charCtl.Move(new Vector3(Input.GetAxis("Horizontal"), , Input.GetAxis("Vertical"))* 0.5f) ;
// charCtl.SimpleMove(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))* 10) ;
}
}

碰撞:

  固定代码,需要添加刚体,也可以不添加刚体,改变函数中判断

    public float pushPower = 2.0F;
void OnControllerColliderHit(ControllerColliderHit hit) {
Rigidbody body = hit.collider.attachedRigidbody;//没有刚体返回空
if (body == null || body.isKinematic)
return; if (hit.moveDirection.y < -0.3F)//被碰撞物体在它下面
return; Vector3 pushDir = new Vector3(hit.moveDirection.x, , hit.moveDirection.z);
body.velocity = pushDir * pushPower;
}

使一人物围绕三个点自动巡逻  转弯时为平滑转弯

两种方法:

  1、 改变transform.forward

  2、 改变transform.rotation

using UnityEngine;
using System.Collections; public class CharControl : MonoBehaviour { CharacterController charCtl ;
public Transform[] point ;
public Transform nextPoint ;
Transform t1 ;
public int index ;
// Use this for initialization
void Start () {
charCtl = GetComponent<CharacterController>() ;
index = ;
nextPoint = point[] ;
} // Update is called once per frame void Update () { // charCtl.Move(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))* 0.5f) ;
// charCtl.SimpleMove(new Vector3(0, 0, Input.GetAxis("Vertical"))* 10) ; // changeForward() ;
changeRotation() ;
}
void changeForward(){
if(Vector3.Distance(IgnoreY(transform.position), IgnoreY(nextPoint.position))>0.2f){
Vector3 direction = (IgnoreY(nextPoint.position)-IgnoreY(transform.position)).normalized ;//must be normalized
transform.forward = Vector3.Lerp(transform.forward, direction, 0.1f);
// charCtl.transform.forward = direction;
// charCtl.transform.LookAt(nextPoint.position);
charCtl.SimpleMove(transform.forward*) ;
}else{
index = (index+)%point.Length ;
nextPoint = point[index] ;
}
}
void changeRotation(){
if(Vector3.Distance(IgnoreY(transform.position), IgnoreY(nextPoint.position))>0.2f){
Vector3 direction = (IgnoreY(nextPoint.position)-IgnoreY(transform.position)).normalized ;
Quaternion rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, 0.1f);
charCtl.SimpleMove(transform.forward*) ; }else{
index = (index+)%point.Length ;
nextPoint = point[index] ; }
} Vector3 IgnoreY(Vector3 x){
return new Vector3(x.x, , x.z) ;
}
public float pushPower = 2.0F;
void OnControllerColliderHit(ControllerColliderHit hit) {
Rigidbody body = hit.collider.attachedRigidbody;//no rigidbody return null
if (body == null || body.isKinematic)
return; if (hit.moveDirection.y < -0.3F)
return; Vector3 pushDir = new Vector3(hit.moveDirection.x, , hit.moveDirection.z);
body.velocity = pushDir * pushPower;
} }

最新文章

  1. 微信小程序-阅读小程序demo
  2. Azure ARM (6) ARM Template简单介绍
  3. 分布式中使用Redis实现Session共享(一)
  4. Unity破解for mac
  5. &amp;&amp;运算符和||运算符的优先级问题
  6. NGINX(一)内存结构
  7. js实现小数点后保留N位并可以四舍五入——js对float数据的处理
  8. Unix/Linux环境C编程入门教程(28) 日期时间那些事儿
  9. Mysql数据库中的EXISTS和NOT EXISTS
  10. C# WinForm dataGridView 技巧小结
  11. awk学习笔记一:基础(转)
  12. Linux_System2
  13. react-native WebView 返回处理 (非回调方法可解决)
  14. 设计模式之责任链模式——Java语言描述
  15. struts2-第一章-基础用法3
  16. 解决删除chrome注册表残留问题
  17. iframe全屏显示
  18. 海南医院帆软报表 最终版本SQL
  19. 转载 转载 转载 数组a[],a,&amp;a之间的区别
  20. Vue: 用 key 管理可复用的元素

热门文章

  1. static 在多台下的特性
  2. linux下securetty文件
  3. Win7系统下VS2008安装SP1补丁解决JQuery无智能提示的问题
  4. ASP.NET中Button控件的CommandName和CommandArgument属性用法
  5. css 强制不换行
  6. sqlalchemy - day2
  7. python 循环、循环设计、循环对象
  8. Node.js学习笔记 01 搭建静态服务器
  9. 刀哥多线程全局队列gcd-09-global_queue
  10. Linux环境PostgreSQL源码编译安装