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

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值