unity中常见的角色控制方法

使用物理引擎(如 Rigidbody)来控制角色

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float jumpForce = 5f;
    private Rigidbody rb;
    private bool isGrounded;

    void Start()
    {
        // 获取角色的 Rigidbody 组件
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        // 获取水平和垂直输入(键盘的方向键或 WASD)
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        // 创建一个新的 Vector3 来表示移动方向
        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

        // 应用移动速度
        rb.velocity = movement * moveSpeed;

        // 检测跳跃输入
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            // 应用跳跃力
            rb.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
        }
    }

    void OnCollisionStay(Collision collision)
    {
        // 检测角色是否在地面上
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }

    void OnCollisionExit(Collision collision)
    {
        // 检测角色是否离开地面
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = false;
        }
    }
}

使用角色控制器CharacterController 组件来控制角色

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float jumpHeight = 1.5f;
    private CharacterController controller;
    private Vector3 playerVelocity;
    private bool groundedPlayer;

    void Start()
    {
        // 获取角色的 CharacterController 组件
        controller = GetComponent<CharacterController>();
    }

    void Update()
    {
        // 检测角色是否在地面上
        groundedPlayer = controller.isGrounded;

        if (groundedPlayer && playerVelocity.y < 0)
        {
            playerVelocity.y = 0f;
        }

        // 获取水平和垂直输入(键盘的方向键或 WASD)
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        // 创建一个新的 Vector3 来表示移动方向
        Vector3 move = new Vector3(moveHorizontal, 0, moveVertical);
        controller.Move(move * Time.deltaTime * moveSpeed);

        // 改变角色面朝移动方向
        if (move != Vector3.zero)
        {
            gameObject.transform.forward = move;
        }

        // 检测跳跃输入
        if (Input.GetButtonDown("Jump") && groundedPlayer)
        {
            playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * Physics.gravity.y);
        }

        // 应用重力
        playerVelocity.y += Physics.gravity.y * Time.deltaTime;
        controller.Move(playerVelocity * Time.deltaTime);
    }
}

通过修改 Transform 组件的位置和旋转来控制角色

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float rotationSpeed = 700f;
    public float jumpForce = 5f;
    private bool isGrounded;
    private Vector3 jump;

    void Start()
    {
        // 初始化跳跃向量
        jump = new Vector3(0.0f, jumpForce, 0.0f);
    }

    void Update()
    {
        // 获取水平和垂直输入(键盘的方向键或 WASD)
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        // 移动角色
        transform.Translate(new Vector3(moveHorizontal, 0, moveVertical) * moveSpeed * Time.deltaTime);

        // 旋转角色
        if (moveHorizontal != 0 || moveVertical != 0)
        {
            Vector3 direction = new Vector3(moveHorizontal, 0, moveVertical);
            Quaternion rotation = Quaternion.LookRotation(direction, Vector3.up);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
        }

        // 检测跳跃输入
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            GetComponent<Rigidbody>().AddForce(jump, ForceMode.Impulse);
        }
    }

    void OnCollisionStay(Collision collision)
    {
        // 检测角色是否在地面上
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }

    void OnCollisionExit(Collision collision)
    {
        // 检测角色是否离开地面
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = false;
        }
    }
}

设置Navigation导航,角色增加Nav Mesh Agent组件,使用SetDestination(目标位置)来进行角色移动

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值