闲谈 这个帖子主要用来解决一些教学视频里讲的移动方法过于单调(WASD和上下左右)的问题,欢迎新手,不懂提问,谢绝装比大神0.0~
一 -- 刚体(Rigidbody)移动
AddForce函数
1.基本使用 (1)获取组件Rb(this,GetCompoment,public) (2)Rb.AddForce(new Vector3(Input.GetAxis("Horizontal") * speed, 0f, Input.GetAxis("Vertical") * speed) ; (3)给刚体增加一个力 (4)相机跟随及旋转 (5)FollowPlayer脚本 (6)挂载在Camera上面
2.Main Camera与Camera保持相对距离(即与Player保持相对距离) (1)[C#] 纯文本查看 复制代码?
12345678 using UnityEngine;[/align]public class FollowPlayer : MonoBehaviour { public GameObject player; void Update () { this.GetComponent<Transform>().position = player.GetComponent<Transform>().localPosition; this.GetComponent<Transform>().Rotate(0, Input.GetAxis("Rotate"), 0); }}
(2)旋转
(3)旋转语句
this.GetComponent<Transform>().Rotate(0, Input.GetAxis("Rotate"), 0);//通过旋转Camera来旋转里面的Main Camera,理论上也可以直接旋转Main Camera,不过代码就会复杂不少
//Rotate是自定义输入,相当于Horizontal,具体设置为
3.旋转之后的前后左右移动
(1)脚本挂载在Player上面
Move()在Update里面执行[C#] 纯文本查看 复制代码?
010203040506070809101112 public void Move(){ Rigidbody arrRigidbody; arrRigidbody = GetComponent<Rigidbody>(); _CameraTransform = _Camera.GetComponent<Transform>(); Vector3 force = new Vector3(Input.GetAxis("Horizontal") * speed, 0, Input.GetAxis("Vertical") * speed); float x = _CameraTransform.rotation.eulerAngles.x; float y = _CameraTransform.rotation.eulerAngles.y; float z = _CameraTransform.rotation.eulerAngles.z; force = Quaternion.Euler(x, y, z) * force; arrRigidbody.AddForce(force);}
(2)Transform.forward是简易版的移动函数,只能准确的向前后移动可能有助于帮助理解这个旋转后移动语句
二 -- 角色控制器(CharacterController)移动
SimpleMove函数
1.基本使用(1)获取组件CC(this,GetCompoment,public)(2)CC.SimpleMove(new Vector3(Input.GetAxis("Horizontal") * speed, 0f, Input.GetAxis("Vertical") * speed) ;
2.旋转将Camera和角色模型一起丢在Player下面就行了MainPlayer组件挂载情况
3.旋转语句transform.Rotate(new Vector3(0, Input.GetAxis("Rotate") * 60 * Time.deltaTime, 0));//Rotate是自定义输入,相当于Horizontal,具体设置为~
4.旋转之后的前后左右移动 Move()在Update里面执行[C#] 纯文本查看 复制代码?
12345678 public void Move()[/align]{ Vector3 force = new Vector3(Input.GetAxis("Horizontal") * speed, 0f, Input.GetAxis("Vertical") * speed); float x = arrplayer.GetComponent<Transform>().localRotation.eulerAngles.x; float y = arrplayer.GetComponent<Transform>().localRotation.eulerAngles.y; float z = arrplayer.GetComponent<Transform>().localRotation.eulerAngles.z; force = Quaternion.Euler(x, y, z) * force; controlle.SimpleMove(force);//controlle为前面获取的CC组件}
5.关于Quaternion,localRotation.eulerAngles不懂的建议看API,因为介绍起来比较麻烦,我是看API之后输出这三个值才大致理解的~一些截图因为没有留备份,不想再截一次了_(:з」∠)_,做了一个总览图,对着看吧(结构都一样)
一 -- 刚体(Rigidbody)移动
AddForce函数
1.基本使用 (1)获取组件Rb(this,GetCompoment,public) (2)Rb.AddForce(new Vector3(Input.GetAxis("Horizontal") * speed, 0f, Input.GetAxis("Vertical") * speed) ; (3)给刚体增加一个力 (4)相机跟随及旋转 (5)FollowPlayer脚本 (6)挂载在Camera上面
2.Main Camera与Camera保持相对距离(即与Player保持相对距离) (1)[C#] 纯文本查看 复制代码?
12345678 using UnityEngine;[/align]public class FollowPlayer : MonoBehaviour { public GameObject player; void Update () { this.GetComponent<Transform>().position = player.GetComponent<Transform>().localPosition; this.GetComponent<Transform>().Rotate(0, Input.GetAxis("Rotate"), 0); }}
(2)旋转
(3)旋转语句
this.GetComponent<Transform>().Rotate(0, Input.GetAxis("Rotate"), 0);//通过旋转Camera来旋转里面的Main Camera,理论上也可以直接旋转Main Camera,不过代码就会复杂不少
//Rotate是自定义输入,相当于Horizontal,具体设置为
3.旋转之后的前后左右移动
(1)脚本挂载在Player上面
Move()在Update里面执行[C#] 纯文本查看 复制代码?
010203040506070809101112 public void Move(){ Rigidbody arrRigidbody; arrRigidbody = GetComponent<Rigidbody>(); _CameraTransform = _Camera.GetComponent<Transform>(); Vector3 force = new Vector3(Input.GetAxis("Horizontal") * speed, 0, Input.GetAxis("Vertical") * speed); float x = _CameraTransform.rotation.eulerAngles.x; float y = _CameraTransform.rotation.eulerAngles.y; float z = _CameraTransform.rotation.eulerAngles.z; force = Quaternion.Euler(x, y, z) * force; arrRigidbody.AddForce(force);}
(2)Transform.forward是简易版的移动函数,只能准确的向前后移动可能有助于帮助理解这个旋转后移动语句
二 -- 角色控制器(CharacterController)移动
SimpleMove函数
1.基本使用(1)获取组件CC(this,GetCompoment,public)(2)CC.SimpleMove(new Vector3(Input.GetAxis("Horizontal") * speed, 0f, Input.GetAxis("Vertical") * speed) ;
2.旋转将Camera和角色模型一起丢在Player下面就行了MainPlayer组件挂载情况
3.旋转语句transform.Rotate(new Vector3(0, Input.GetAxis("Rotate") * 60 * Time.deltaTime, 0));//Rotate是自定义输入,相当于Horizontal,具体设置为~
4.旋转之后的前后左右移动 Move()在Update里面执行[C#] 纯文本查看 复制代码?
12345678 public void Move()[/align]{ Vector3 force = new Vector3(Input.GetAxis("Horizontal") * speed, 0f, Input.GetAxis("Vertical") * speed); float x = arrplayer.GetComponent<Transform>().localRotation.eulerAngles.x; float y = arrplayer.GetComponent<Transform>().localRotation.eulerAngles.y; float z = arrplayer.GetComponent<Transform>().localRotation.eulerAngles.z; force = Quaternion.Euler(x, y, z) * force; controlle.SimpleMove(force);//controlle为前面获取的CC组件}
5.关于Quaternion,localRotation.eulerAngles不懂的建议看API,因为介绍起来比较麻烦,我是看API之后输出这三个值才大致理解的~一些截图因为没有留备份,不想再截一次了_(:з」∠)_,做了一个总览图,对着看吧(结构都一样)