【问题标题】:Rigidbody manipulation not working on Y axis? (Unity3D 2020.1)刚体操作在 Y 轴上不起作用? (Unity3D 2020.1)
【发布时间】:2021-02-04 15:24:14
【问题描述】:

这很奇怪。

我一直在更深入地了解刚体,但我一辈子都做不到,让它跳起来。我完全复制了我在 youtube 教程上看到的内容,它适用于 youtuber,但不适用于我。

更多 C# 英语 - 我试图通过用户输入在 y 轴上添加 .AddForce(ForceMode.TriedEverything)。它不会始终如一地工作。有时它更高,有时更低,大多数时候什么都没有。这很奇怪。我什至没有设置 isGrounded 或任何其他限制。只需“如果用户输入,则添加强制”。这是第 1 个案例

案例 2 简单得多,但从不工作:它基于将速度设置为仅操纵 y 轴的新向量。我会分享代码。 “jumpForce”设置为 12,但我也试过 500。没有不同。这个案例确实有一个“接地”的条件,但没有它也无法工作。无论如何,两个打印语句都会被执行。

两种情况都在 Update 和 FixedUpdate 中进行了测试,没有区别。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMotor : MonoBehaviour
{
    public LayerMask layerMask = default;
    public bool grounded;

    [SerializeField] float moveSpeed = 7f;
    [SerializeField] float jumpForce = 12f;

    

    Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {   
        float x = Input.GetAxisRaw("Horizontal") * moveSpeed;
        float y = Input.GetAxisRaw("Vertical") * moveSpeed;


        Vector3 movePosition = transform.right * x + transform.forward * y;
        Vector3 newPosition = new Vector3(movePosition.x, rb.velocity.y, movePosition.z);

        grounded = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), 0.4f, layerMask);
        
        if (Input.GetKeyDown(KeyCode.Space)&& grounded)
        {
            print("reached if statement");
            rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
            print("jumpingjumping");
        }

        rb.velocity = newPosition;

    }

    void FixedUpdate()
    {

    }


}

谢谢各位。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    问题在于代码的顺序。您在跳转语句之前设置 newPosition 变量,因此 y 速度为 0,然后您将 y 速度设置为跳转速度,但之后您将速度设置为 newPosition,它的速度仍然为 0,因为它是之前设置的跳转语句。这是工作代码:

        public LayerMask layerMask = default;
    public bool grounded;
    
    [SerializeField] float moveSpeed = 7f;
    [SerializeField] float jumpForce = 12f;
    
    
    
    Rigidbody rb;
    
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    
    void Update()
    {
        float x = Input.GetAxisRaw("Horizontal") * moveSpeed;
        float y = Input.GetAxisRaw("Vertical") * moveSpeed;
    
    
        Vector3 movePosition = transform.right * x + transform.forward * y;
    
        grounded = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), 0.4f, layerMask);
    
        if (Input.GetKeyDown(KeyCode.Space) && grounded)
        {
            print("reached if statement");
            rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
            print("jumpingjumping");
        }
    
        Vector3 newPosition = new Vector3(movePosition.x, rb.velocity.y, movePosition.z);
        rb.velocity = newPosition;
    
    }
    
    void FixedUpdate()
    {
    
    }
    

    希望这对您有所帮助!

    【讨论】:

    • 嘿伙计,谢谢!我用这种类型的代码修复了它,但由于某种原因从未看到你的评论,我以为线程死了:/
    猜你喜欢
    • 2021-02-08
    • 2022-01-06
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    相关资源
    最近更新 更多