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