赞
踩
- 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;
- }
- }
- }
- 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);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。