我在unity中制作了一個3D游戲,所以我為我的角色的移動做了一個cs腳本,行走和移動攝像機都很正常,但是當我加入跳躍功能時,它有一個延遲。你可以按5次跳躍按鈕,但沒有結果。然后你再按一次,它就跳起來了。我不明白為什么會出現這種情況。
using System.Collections;
using System.Collections.Generic;
public class PlayerMovement : MonoBehaviour
{
public CharacterController characterController;
public int speed = 6;
public float gravity = 9.87f;
private float verticalspeed = 0;
private Vector3 moveDirection = Vector3.0。
public Transform Camera;
public float Sensitivity = 2f;
public float uplimit = 50;
public float downlimit = -50;
public float jumpspeed = 5.0f;
void Update()
{
move()。
cameramove()。
void cameramove()。
{
float horizontal = Input.GetAxis("Mouse X"/span>)。
float vertical = Input.GetAxis("Mouse Y") 。
transform.Rotate(0, horizontal * Sensitivity, 0) 。
Camera.Rotate(-vertical * Sensitivity, 0, 0) 。
Vector3 currentRotation = Camera.localEulerAngles;
if (currentRotation.x > 180) currentRotation.x -= 360;
currentRotation.x = Mathf.Clamp(currentRotation.x, uplimit, downlimit)。
Camera.localRotation = Quaternion.Euler(currentRotation)。
}
void move()?
{
float horizontalMove = Input.GetAxis("Horizontal"/span>)。
float verticalMove = Input.GetAxis("Vertical")。
if (characterController.isGrounded) verticalspeed = 0;
else verticalspeed -= gravity * Time.deltaTime;
Vector3 gravityMove = new Vector3(0, verticalspeed, 0)。
Vector3 move = transform.forward * verticalMove transform.right * horizontalMove;
characterController.Move(speed * Time.deltaTime * move gravityMove * Time.deltaTime)。
if (characterController.isGrounded && Input.GetButton("Jump")
{
moveDirection.y = jumpspeed;
}
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime)。
}
}
}
uj5u.com熱心網友回復:
假設你的角色有一個RigidBody分配給它,你可以在你的腳本上參考它作為,
public class PlayerMovement : MonoBehaviour
{
private Rigidbody myrigidbody;
void Start () {
myrigidbody = GetComponent<Rigidbody>()。
}
然后你可以把jumpspeed作為一個Vector3來傳遞,可能你還需要在這里觸發你的影片。
if (characterController.isGrounded && Input.GetButton("Jump")
{
characterController.isGrounded = false;
myrigidbody.AddForce(new Vector3(0, jumpspeed, 0))。)
//Trigger your animation, myanimator.SetTrigger("jump");
uj5u.com熱心網友回復:
Unity角色控制器的移動檔案也包含了跳躍功能。
using System.Collections;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
private CharacterController控制器。
private Vector3 playerVelocity;
private bool groundedPlayer;
private float playerSpeed = 2.0f;
private float jumpHeight = 1.0f;
private float gravityValue = 9.81f;
private void Start()
{
controller = gameObject.AddComponent<CharacterController>()。
}
void Update()
{
groundedPlayer = controller.isGrounded;
if (groundedPlayer & & playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") )。)
控制器.移動(move * Time.deltaTime * playerSpeed)。
if (move != Vector3.0)
{
gameObject.transform.forward = move。
}
//改變玩家的高度位置。
if (Input.GetButtonDown("Jump") & & groundedPlayer)
{
playerVelocity.y = Mathf.Sqrt(jumpHeight * -3.0f * gravityValue)。
}
playerVelocity.y = gravityValue * Time.deltaTime;
控制器.移動(playerVelocity * Time.deltaTime)。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/317233.html
標籤:
