Unity 控制物体移动

本文介绍了在Unity3D中移动物体的三种方法:1)通过直接修改物体位置;2)通过施加力模拟物理运动;3)使用CharacterController进行角色移动及碰撞检测。第一种不遵循物理规则,第二种考虑了物理效应,第三种适合游戏角色行为。
摘要由CSDN通过智能技术生成

目录

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,然后再把代码赋予物体

 

二、区别

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

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱颜辞镜花辞树>

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值