1. 程式人生 > >2018-10-16 Rigidbody剛體,移動物件的方法MovePosion及transform.Translate()移動物件

2018-10-16 Rigidbody剛體,移動物件的方法MovePosion及transform.Translate()移動物件

    private Rigidbody rigidbody;//宣告剛體物件
    private Transform tran;//申明物件Transforma物件
    public float speed = 0.2f;
	// Use this for initialization
	void Start ()
    {
        //獲取元件
        rigidbody = gameObject.GetComponent<Rigidbody>();
        tran = gameObject.GetComponent<Transform>();
	}
	
	// Update is called once per frame
	void Update () {
        PlayerMove();

    }

    //物件移動方法
    void PlayerMove()
    {
        if(Input.GetKey(KeyCode.W))
        {
            rigidbody.MovePosition(tran.position + Vector3.forward * speed);
        }
        if (Input.GetKey(KeyCode.S))
        {
            rigidbody.MovePosition(tran.position + Vector3.back * speed);
        }
        if (Input.GetKey(KeyCode.A))
        {
            rigidbody.MovePosition(tran.position + Vector3.left * speed);
        }
        if (Input.GetKey(KeyCode.D))
        {
            rigidbody.MovePosition(tran.position + Vector3.right * speed);
        }
    }

    //物件銷燬方法,通過標籤名
    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag=="Box")
        {
            GameObject.Destroy(collision.gameObject,1);
        }
    }

 

    private Rigidbody rigidbody;//宣告剛體物件
    private Transform tran;//申明物件Transforma物件
    public float speed = 0.2f;
    // Use this for initialization
    void Start ()
    {
        //獲取元件
        rigidbody = gameObject.GetComponent<Rigidbody>();
        tran = gameObject.GetComponent<Transform>();
    }
    
    // Update is called once per frame
    void Update () {
        PlayerMove();

    }

    //物件移動方法
    void PlayerMove()
    {
        if(Input.GetKey(KeyCode.W))
        {
            rigidbody.MovePosition(tran.position + Vector3.forward * speed);
        }
        if (Input.GetKey(KeyCode.S))
        {
            rigidbody.MovePosition(tran.position + Vector3.back * speed);
        }
        if (Input.GetKey(KeyCode.A))
        {
            rigidbody.MovePosition(tran.position + Vector3.left * speed);
        }
        if (Input.GetKey(KeyCode.D))
        {
            rigidbody.MovePosition(tran.position + Vector3.right * speed);
        }
    }

    //物件銷燬方法,通過標籤名
    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag=="Box")
        {
            GameObject.Destroy(collision.gameObject,1);
        }
    }

 

 

void MovePlayer()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward*Time.deltaTime*4,Space.World);
        }

        if (Input.GetKey(KeyCode.A))
        {
            //transform.Rotate(Vector3.down*3);
            transform.Translate(Vector3.left * Time.deltaTime * 4, Space.World);
        }
        if (Input.GetKey(KeyCode.D))
        {
            //transform.Rotate(Vector3.up*3);
            transform.Translate(Vector3.right * Time.deltaTime * 4, Space.World);
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(Vector3.back * Time.deltaTime * 4, Space.World);
        }

    }