英文:
Player too slow, can't jump, and moves fast diagonally
问题
My current issues are that the player is extremely slow no matter what I set my moveSpeed to, the player won't jump, and the player moves fast when moving diagonally. The script is shown below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 10f;
public float jumpForce = 10f;
public float groundCheckRadius = 0.2f;
public LayerMask groundLayer;
private Rigidbody rb;
private bool isGrounded;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float horizontalInput = Input.GetAxisRaw("Horizontal"); // 获取水平输入
float verticalInput = Input.GetAxisRaw("Vertical"); // 获取垂直输入
Vector3 moveDirection = (transform.right * horizontalInput + transform.forward * verticalInput) * moveSpeed;
rb.MovePosition(transform.position + moveDirection * Time.deltaTime); // 使用Rigidbody的MovePosition方法移动玩家
isGrounded = Physics.CheckSphere(transform.position, groundCheckRadius, groundLayer);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
}
-
I've tried changing Update to FixedUpdate, and Time.deltaTime to Time.fixedDeltaTime (resulted in player moving insanely fast no matter what moveSpeed was set to.
-
I've tried changing Input.GetAxisRaw to Input.GetAxis, resulting in movement being too smooth, but I'm going for responsiveness.
-
I've tried removing Time.deltaTime, resulting in choppiness and the player clipping through walls.
-
I've tried changing the method of jumping to changing rb.velocity.y to jumpForce when Space is pressed and the player is grounded.
英文:
My current issues are that the player is extremely slow no matter what I set my moveSpeed to, the player won't jump, and the player moves fast when moving diagonally. The script is shown below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 10f;
public float jumpForce = 10f;
public float groundCheckRadius = 0.2f;
public LayerMask groundLayer;
private Rigidbody rb;
private bool isGrounded;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float horizontalInput = Input.GetAxisRaw("Horizontal"); //Get horizontal input
float verticalInput = Input.GetAxisRaw("Vertical"); //Get vertical input
Vector3 moveDirection = (transform.right * horizontalInput + transform.forward * verticalInput) * moveSpeed;
rb.MovePosition(transform.position + moveDirection * Time.deltaTime); //Move the player using Rigidbody's MovePosition method
isGrounded = Physics.CheckSphere(transform.position, groundCheckRadius, groundLayer);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
}
-
I've tried changing Update to Fixed update, and Time.deltaTime to Time.fixedDeltaTime (resulted in player moving insanely fast no matter what moveSpeed was set to
-
I've tried changing Input.GetAxisRaw to Input.GetAxis, resulting in movement being too smooth, but I'm going for responsiveness
-
I've tried removing Time.deltaTime, resulting in choppiness and the player clipping through walls.
-
I've tried changing the method of jumping to changing rb.velocity.y to jumpForce when Space is pressed and the player is grounded.
答案1
得分: 1
检查Inspector选项卡中的Unity脚本。通常情况下,您在Inspector选项卡中设置的变量值会覆盖您在脚本中设置的值。
英文:
Check the Unity script inside the Inspector tab. Typically, the variable value you set in the inspector tab overrules the value you set in script.
答案2
得分: 0
切换到使用速度而不是变换修复了物理问题,通过检视器而不是脚本编辑移动速度修复了移动缓慢,将速度向量归一化修复了对角移动速度过快。
英文:
Switching to moving using velocity instead of transform fixed the physics issues, editing movespeed through the inspector instead of the script fixed the slow movement, and normalizing the velocity vector fixed diagonal movement being faster.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论