1. 程式人生 > >Unity3D物體移動及Player移動的通用方法

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);

    }

兩者區別