赞
踩
- 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;
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- 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);
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- 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;
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。