Unity 控制物体移动

目录

1、通过改变物体的位置使物体移动

2、通过给物体施加力使物体移动

3、移动characterController以及碰撞检测


一、相关代码展示

1、通过改变物体的位置使物体移动

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

public class move : MonoBehaviour
{
    private Vector3 m_camRot;
    private Transform m_camTransform;
    private Transform m_transform;
    public float m_movSpeed = 10;//移动系数
    // Start is called before the first frame update
    void Start()
    {
        m_camTransform = Camera.main.transform;
        m_transform=GetComponent<Transform>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0)){
            //获取鼠标移动距离
            float rh = Input.GetAxis("Mouse X");
            float rv = Input.GetAxis("Mouse Y");
        }
        float xm = 0, ym = 0, zm = 0;
        if (Input.GetKey(KeyCode.W))
        {
            zm += m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            zm -= m_movSpeed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.A))
        {
            xm -= m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.D))
        {
            xm += m_movSpeed * Time.deltaTime;
        }
        if(Input.GetKey(KeyCode.Space) && m_transform.position.y<=3) {
        ym+= m_movSpeed * Time.deltaTime;
        }
        if(Input.GetKey(KeyCode.F) && m_transform.position.y >= 1)
        {
            ym -= m_movSpeed * Time.deltaTime;
        }
        m_transform.Translate(new Vector3(xm, ym, zm), Space.Self);
    }
}

2、通过给物体施加力使物体移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class move : MonoBehaviour
{
    public float speed=10;
 void FixedUpdate()
    {
        float moveh = Input.GetAxis("Horizontal");
        float movev = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveh, 0.0f, movev);
        GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
    }
}

3、移动characterController 以及碰撞检测

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

public class FPSInput : MonoBehaviour
{
    public float speed = 6.0f;
    public float gravity = -9.8f;
    private CharacterController _charController;
    // Start is called before the first frame update
    void Start()
    {
        _charController = GetComponent<CharacterController>();//使用附加到相同对象上的其他组件
    }

    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxis("Horizontal") * speed;
        float z = Input.GetAxis("Vertical") * speed;
        Vector3 movement = new Vector3(x, 0, z);
        movement = Vector3.ClampMagnitude(movement, speed);//使对角移动的速度和沿轴移动的速度一样
        movement.y = gravity;
        movement *= Time.deltaTime;
        movement = transform.TransformDirection(movement);//本地坐标变为全局坐标
        _charController.Move(movement);//character通过movement向量移动
    }
}

注:使用时首先要给物体添加CharacterController,然后再把代码赋予物体

二、区别

第一种是直接改变物体的坐标来使物体运动,不符合一般的物理规律,第二种是给物体施加了力,运动符合物理学规律,如加速,惯性等。第三种是让对象移动起来更像游戏中的角色,包括和墙壁碰撞

猜你喜欢

转载自blog.csdn.net/lxy20011125/article/details/129316692