【Unity】Unity移动,跳跃(这篇使用Rigidbody和CharacterController两种方式)

1,Rigidbody

using UnityEngine;

public class Move : MonoBehaviour
{
    /// <summary>
    /// 移动速度
    /// </summary>
    public float moveSpeed = 1f;
    /// <summary>
    /// 转弯速度
    /// </summary>
    public float rotateSpeed = 60f;
    /// <summary>
    /// 跳跃力
    /// </summary>
    public float jumpVelocity = 5.0f;

    private float vInput;
    private float hInput;
    private float JInput;

    private Rigidbody _rb;

    /// <summary>
    /// 判断触碰地面
    /// </summary>
    private bool _isGrounded;

    void Start()
    {
        _rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        vInput = Input.GetAxis("Vertical") * moveSpeed;
        hInput = Input.GetAxis("Horizontal") * moveSpeed;
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //判断是否在地面
            if (!_isGrounded)
            {
                JInput = jumpVelocity;
            }
        }
    }
    void FixedUpdate()
    {

        //跳跃 添加一个向上的瞬间力ForceMode.Impulse,要跳的越高jumpVelocity值就越大
        _rb.AddForce(Vector3.up * JInput, ForceMode.Impulse);
        //执行完Update的 JInput = jumpVelocity,并且上行逻辑执行完毕后 执行 JInput = 0f,物体下落
        JInput = 0f;


        //移动
        if (Mathf.Abs(vInput) != 0 || Mathf.Abs(hInput) != 0)
        {
            _rb.MovePosition(this.transform.position + new Vector3(hInput * Time.fixedDeltaTime, 0, vInput * Time.fixedDeltaTime));
        }

        //旋转
        Vector3 rotation_V = new Vector3(vInput, 0, transform.position.x) * rotateSpeed;
        Vector3 rotation_H = new Vector3(transform.position.z, 0, hInput) * rotateSpeed;


        Quaternion angleRot_V = Quaternion.Euler(rotation_V * Time.fixedDeltaTime);
        Quaternion angleRot_H = Quaternion.Euler(rotation_H * Time.fixedDeltaTime);

        if (Mathf.Abs(vInput) != 0)
        {
            _rb.MoveRotation(_rb.rotation * angleRot_V);
        }
        if (Mathf.Abs(hInput) != 0)
        {
            _rb.MoveRotation(_rb.rotation * angleRot_H);
        }


    }
    /// <summary>
    /// 碰撞检测刚触碰到时_isGrounded = false;可以跳跃 
    /// </summary>
    /// <param name="collision"></param>
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag.Equals("Grounded"))
        {
            _isGrounded = false;
        }
    }
    /// <summary>
    /// 碰撞检测触碰结束时_isGrounded = true;不可以跳跃 
    /// </summary>
    /// <param name="collision"></param>
    private void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.tag.Equals("Grounded"))
        {
            _isGrounded = true;
        }
    }
}

2,CharacterController

using UnityEngine;

public class Move : MonoBehaviour
{
    private CharacterController controller;
    private Vector3 Direction;
    private Vector3 JumpDown;
    private float vInput;
    private float hInput;
    /// <summary>
    /// 移动速度
    /// </summary>
    private float moveSpeed = 1.5f;
    /// <summary>
    /// 跳跃力度
    /// </summary>
    private float jumpSpeed = 5f;
    /// <summary>
    /// 转弯速度
    /// </summary>
    private float rotateSpeed = 180f;
    /// <summary>
    /// 跳跃高度
    /// </summary>
    private float jumpHeight = 7f;
    /// <summary>
    /// 重力
    /// </summary>
    private float Gravity = 18f;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        Direction = Vector3.zero;
    }
    void Update()
    {
        //判断是否在地面
        if (controller.isGrounded)
        {
            vInput = Input.GetAxis("Vertical");
            hInput = Input.GetAxis("Horizontal");
            Direction = new Vector3(hInput, 0f, vInput);
            Direction *= moveSpeed;
            //旋转
            Vector3 rotation_V = Vector3.right * vInput * rotateSpeed * Time.deltaTime;
            Vector3 rotation_H = Vector3.back * hInput * rotateSpeed * Time.deltaTime;
            if (Mathf.Abs(vInput) != 0)
            {
                transform.Rotate(rotation_V, Space.World);
            }
            if (Mathf.Abs(hInput) != 0)
            {
                transform.Rotate(rotation_H, Space.World);
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {
                //跳跃高度
                JumpDown.y = jumpHeight;
            }
        }
        else
        {
            //不在地面随时间施加重力
            JumpDown.y -= Gravity * Time.deltaTime;
        }
        //移动加跳跃
        controller.Move((Direction + JumpDown) * jumpSpeed * Time.deltaTime);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 当然可以使用Unity编写一个移动CharacterController。下面是一个基本的示例代码,可在Unity中创建一个名为"Player"的GameObject,然后将以下代码添加到该对象的脚本组件中,即可实现角色的移动: ``` using UnityEngine; public class PlayerController : MonoBehaviour { public float moveSpeed = 5.0f; // 移动速度 private CharacterController controller; // CharacterController组件 private Vector3 moveDirection = Vector3.zero; // 移动方向 void Start() { controller = GetComponent<CharacterController>(); // 获取CharacterController组件 } void Update() { float horizontal = Input.GetAxis("Horizontal"); // 获取水平方向上的输入 float vertical = Input.GetAxis("Vertical"); // 获取垂直方向上的输入 // 计算移动方向 moveDirection = new Vector3(horizontal, 0.0f, vertical); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= moveSpeed; // 将移动方向应用到CharacterController组件上 controller.Move(moveDirection * Time.deltaTime); } } ``` 这个示例代码中,我们定义了一个名为“PlayerController”的脚本,该脚本会在每一帧中获取玩家的输入,然后根据输入计算出移动方向,并将移动方向应用到CharacterController组件上。注意,这里我们使用了Time.deltaTime来控制移动速度,这样可以保证在不同的帧率下,角色的移动速度保持一致。 ### 回答2: Unity是一款强大的游戏开发引擎,可以用来创建各种不同类型的游戏。编写一个移动character controller可以让玩家在游戏中控制角色的移动。 在Unity中,编写一个character controller需要以下几个步骤: 1. 创建一个新的GameObject并将其重命名为"Player",用来表示角色。将该GameObject添加一个Capsule Collider组件,以便其他物体与角色互动碰撞。 2. 添加一个Rigidbody组件,以便在角色移动时受到物理引擎影响。确保将Rigidbody的"Use Gravity"选项禁用,这样角色就不会受到重力影响。 3. 创建一个C#脚本,并将其附加到Player对象上。在脚本中,声明一个变量来控制角色的移动速度,例如"moveSpeed"。 4. 在脚本中,使用Input.GetAxis()函数来获取玩家的输入,例如水平和垂直轴的输入。将输入乘以移动速度,得到角色在每个方向上的移动速度。 5. 使用Rigidbody组件的velocity属性来将计算得到的移动速度应用到角色上。通过调用Rigidbody的MovePosition()函数并传递角色的新位置,使角色移动。 6. 为了让角色在空中能跳跃,你可以添加一个额外的检测机制,检测角色是否与地面接触。当玩家按下跳跃键时,在脚本中添加一个适当的力量到角色的Y轴速度上。 7. 如果你还想要角色的旋转,你可以使用Input.GetAxis()函数来获取玩家的转向输入,并将其应用到角色上的Rigidbody组件的rotation属性上。 通过以上步骤,你就可以使用Unity编写一个简单的移动character controller了。当你在游戏场景中运行游戏并玩家点击键盘上的WASD键时,角色就可以在场景中移动了。 ### 回答3: 使用Unity写一个移动Character Controller可以通过以下步骤完成: 1. 创建一个新的Unity项目并导入所需的资源(例如角色模型、动画等)。 2. 创建一个空的Game Object作为角色控制器,并将其添加到场景中。 3. 添加一个Rigidbody组件到角色控制器上,用于处理物理运动。 4. 添加一个Capsule Collider组件到角色控制器上,以便处理碰撞。 5. 创建脚本文件并将其附加到角色控制器上。 6. 在脚本中,定义角色的移动速度、旋转速度等变量,并使用Input.GetAxis函数获取玩家输入。 7. 在Update函数中,根据玩家输入控制角色的移动和旋转。 8. 使用Rigidbody的AddForce函数将力应用于角色控制器,以使其移动。 9. 在角色控制器的脚本中,可以添加额外的功能,例如跳跃、攻击等。 10. 测试和调试角色控制器,确保角色能够按照预期移动和旋转。 11. 可以根据需要添加更多功能,例如动画、摄像机跟随等。 通过以上步骤,你可以使用Unity编写一个移动Character Controller,并通过脚本控制角色的移动和旋转。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值