当前位置:   article > 正文

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

unity rigidbody与character

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/602379?site
推荐阅读
相关标签
  

闽ICP备14008679号