Unity3D物体移动及Player移动的通用方法
物体移动的常用方法
一、transform.Translate
- transform.Translate(Vector3.forward *MoveSpeed * Time.deltaTime, Space.transform);
二、velocity(刚体)
- gameObject.GetComponent<Rigidbody>().velocity = Vector3.forward * MoveSpeed;
三、rigbody.MovePosition()
- //让物体向前运动Time.deltaTime距离
- rb.MovePosition(transform.position + transform.forward * Time.deltaTime);
四、Vector3.MoveTowards()
- public int MoveSpeed = 10;
- Vector3 target;
- void Start () {
- target = new Vector3(20, transform.position.y, 20);
- }
- void Update () {
- transform.position = Vector3.MoveTowards(transform.position, target, MoveSpeed * Time.deltaTime);
- }
五、Lerp()
1.使用Mathf.Lerp()函数
- gameObject.transform.localPosition = new Vector3(
- Mathf.Lerp(transform.position.x, Target.x, MoveSpeed * Time.deltaTime),
- Mathf.Lerp(transform.position.y, Target.y, MoveSpeed * Time.deltaTime),
- Mathf.Lerp(transform.position.z, Target.z, MoveSpeed * Time.deltaTime));
2.使用Vector3.Lerp()
- gameObject.transform.localPosition = Vector3.Lerp(transform.position, Target, MoveSpeed * Time.deltaTime);
六、使用SmoothDamp() 平滑阻尼(摄像机)
- //平滑地移动摄像机朝向目标位置 smoothTime:大约花费时间
- transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
Player移动的常用方法
一、rigidbody.MovePosition() 控制物体上下左右移动
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- rigidbody.MovePosition(transform.position + new Vector3(h, 0, v) * speed * Time.deltaTime);
二、rigidbody.velocity控制物体 上下左右移动(坦克大战,控制物体转向移动)
- //获取轴 每个轴对应于键盘或者鼠标的某种动作 Horizontal为x轴 Vertical为z轴
- float v = Input.GetAxis("VerticalPlay"+Number);
- //刚体速度向量 z轴向前*
- m_rigidbody.velocity = transform.forward * v * speed;
- float h = Input.GetAxis("HorizontalPlay"+Number);
- //刚体角度速度
- m_rigidbody.angularVelocity = transform.up * h * angularSpeed;
三、transform.Translate()上下左右移动
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- transform.Translate(new Vector3(h, 0, v) * speed * Time.deltaTime);
四、点击鼠标,物体移动到鼠标的位置
- protected Transform _transform;
- protected Vector3 targetPos;//目标位置
- // Use this for initialization
- void Start()
- {
- _transform = this.transform;
- targetPos = this._transform.position;
- }
- void MoveTo()
- {
- if (Input.GetMouseButton(0))
- {
- //获得鼠标屏幕位置
- Vector3 mousePos = Input.mousePosition;
- //将屏幕位置转为射线
- Ray ray = Camera.main.ScreenPointToRay(mousePos);
- //用来记录射线碰撞记录
- RaycastHit hitInfo;
- //产生射线
- bool isCast = Physics.Raycast(ray, out hitInfo, 1000, inputMask);
- if (isCast)
- {
- //如果射中目标,记录射线碰撞点
- targetPos = hitInfo.point;
- }
- }
- //使用Vector3提供的MoveTowards函数,获得朝目标移动的位置
- Vector3 pos = Vector3.MoveTowards(this._transform.position, targetPos, speed * Time.deltaTime);
- //更新当前位置
- this._transform.position = pos;
- }
五、rigidbody2D.AddForce() 2D游戏添加力左右移动物体(忍者跑酷)
- if (isSlide == false)
- {
- float h = Input.GetAxis("Horizontal");
- Vector2 velocity = rigidbody2D.velocity;
- if (h > 0.05f)
- {
- rigidbody2D.AddForce(Vector2.right * force_move);
- }
- else if (h < -0.05f)
- {
- rigidbody2D.AddForce(-Vector2.right * force_move);
- }
六、CharacterController组建
1.Move移动
- public float speed = 6.0F;
- public float jumpSpeed = 8.0F;
- public float gravity = 20.0F;
- private Vector3 moveDirection = Vector3.zero;
- void Update() {
- CharacterController controller = GetComponent<CharacterController>();
- if (controller.isGrounded) {
- moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection *= speed;
- if (Input.GetButton("Jump"))
- moveDirection.y = jumpSpeed;
- }
- moveDirection.y -= gravity * Time.deltaTime;
- controller.Move(moveDirection * Time.deltaTime);
- }
2.SimpleMove移动
- public float speed = 3.0F;
- public float rotateSpeed = 3.0F;
- void Update() {
- CharacterController controller = GetComponent<CharacterController>();
- transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
- Vector3 forward = transform.TransformDirection(Vector3.forward);
- float curSpeed = speed * Input.GetAxis("Vertical");
- controller.SimpleMove(forward * curSpeed);
- }
两者区别