unity3D----物体移动的三种简单方式

一、不添加刚体组件,使物体移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour
{
    private Transform t;

    void Start()
    {
        t = this.GetComponent<Transform>();  //获取本身和所有子子物体
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {       
           t.Translate(Vector3.forward * Time.deltaTime * 6, Space.World);
        }
        if (Input.GetKey(KeyCode.S))
        {
            t.Translate(Vector3.back * Time.deltaTime * 6, Space.World);
        }
        if (Input.GetKey(KeyCode.A))
        {
            t.Translate(Vector3.left * Time.deltaTime * 6, Space.World);
        }
        if (Input.GetKey(KeyCode.D))
        {
            t.Translate(Vector3.right * Time.deltaTime * 6, Space.World);
        }
    }
}

1.首先,我们通过t = this.GetComponent<Transform>() 获取物体本身的坐标。

2.然后通过Translate()这个方法可以使得物体移动,里面有Vector3,
(Vector3是一个三维向量,在unity中用于传递3D位置和方向),通过在后面加入Time.deltaTime来使得物体移动更加的平滑。

3.里面还有一个Space.World,代表着是以世界坐标系来移动的,与之相对的还有Space.Self,它代表着以自身坐标系来移动。

 

 

二、通过添加刚体,是用MovePosition方法来移动物体

 

1.首先我们需要给物体添加刚体。

使用这个方法,物体移动时就不会穿透其他的物体

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour
{
    //创建一个私有的Rigidbody和Transform,用于接收Rigidbody和Transform的组件
    private Rigidbody m_rigidbody;
    private Transform m_transform;

    void Start()
    {
        //拿到Rigidbody和Transform的组件
        m_rigidbody = this.GetComponent<Rigidbody>();//刚体组件是用来移动物体
        m_transform = this.GetComponent<Transform>();//使用Transform的组件是因为我们采用的movePosition方法是用改变Transform来移动的
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            m_rigidbody.MovePosition(m_transform.position + Vector3.forward * 0.05f);
        }
        if (Input.GetKey(KeyCode.S))
        {
            m_rigidbody.MovePosition(m_transform.position + Vector3.back * 0.05f);
        }
        if (Input.GetKey(KeyCode.A))
        {
            m_rigidbody.MovePosition(m_transform.position + Vector3.left * 0.05f);
        }
        if (Input.GetKey(KeyCode.D))
        {
            m_rigidbody.MovePosition(m_transform.position + Vector3.right * 0.05f);
        }
    }
}

 

 

三、用addforce方法来移动物体

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour
{
    //创建一个私有的Rigidbody用于接收Rigidbody
    private Rigidbody m_rigidbody;

    void Start()
    {
        m_rigidbody = this.GetComponent<Rigidbody>(); //拿到Rigidbody组件
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            m_rigidbody.AddRelativeForce(Vector3.forward*30, ForceMode.Force);//这个是以物体自身坐标系来移动
            m_rigidbody.AddForce(Vector3.forward*30, ForceMode.Force);//这个是以世界坐标系来移动
        }
        if (Input.GetKey(KeyCode.S))
        {
            m_rigidbody.AddForce(Vector3.back*30, ForceMode.Force);
        }
        if (Input.GetKey(KeyCode.A))
        {
            m_rigidbody.AddForce(Vector3.left*30, ForceMode.Force);
        }
        if (Input.GetKey(KeyCode.D))
        {
            m_rigidbody.AddForce(Vector3.right*30, ForceMode.Force);
        }
    }
}

ForceMode 是一个枚举结构,里面有:

ForceMode.Force 是给物体添加一个作用力来使物体移动。

ForceMode.Acceleration 表示添加一个可持续加速度到刚体,会忽略它的质量。

ForceMode.Impulse 表示添加一个瞬间冲击力到刚体,会使用它的质量。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值