玩家太慢,无法跳跃,并且以对角线移动得很快。

huangapple go评论60阅读模式
英文:

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);
        }
    }
}
  1. 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.

  2. I've tried changing Input.GetAxisRaw to Input.GetAxis, resulting in movement being too smooth, but I'm going for responsiveness.

  3. I've tried removing Time.deltaTime, resulting in choppiness and the player clipping through walls.

  4. 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&lt;Rigidbody&gt;();
    }

    // Update is called once per frame
    void Update()
    {
        float horizontalInput = Input.GetAxisRaw(&quot;Horizontal&quot;); //Get horizontal input
        float verticalInput = Input.GetAxisRaw(&quot;Vertical&quot;); //Get vertical input

        Vector3 moveDirection = (transform.right * horizontalInput + transform.forward * verticalInput) * moveSpeed;
        rb.MovePosition(transform.position + moveDirection * Time.deltaTime); //Move the player using Rigidbody&#39;s MovePosition method

        isGrounded = Physics.CheckSphere(transform.position, groundCheckRadius, groundLayer);

        if (Input.GetKeyDown(KeyCode.Space) &amp;&amp; isGrounded)
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
        }
    }
}
  1. 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

  2. I've tried changing Input.GetAxisRaw to Input.GetAxis, resulting in movement being too smooth, but I'm going for responsiveness

  3. I've tried removing Time.deltaTime, resulting in choppiness and the player clipping through walls.

  4. 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.

huangapple
  • 本文由 发表于 2023年4月17日 03:37:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定