【问题标题】:Moving a rigid body in the direction it's facing in Unity and C#在 Unity 和 C# 中沿其面对的方向移动刚体
【发布时间】:2020-08-12 17:34:14
【问题描述】:

我一直在尝试使用我能找到的几乎所有方法(甚至使用可怕的 transform.translate)来让它工作,但我似乎无法让它工作。这只是代码的草稿,如果还有其他方法可以解决,我会更改一些内容。

目前,他几乎没有移动(看起来他不知何故被卡在了地板上。)我对使用刚体移动物体还很陌生,所以我对如何解决这个问题几乎一无所知。

这是我最新破解的脚本:

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

public class PlayerTest : MonoBehaviour
{
    public float speed = 10.0f;
    public Rigidbody rb;
    public Vector3 movement;

// Start is called before the first frame update
void Start()
{
    rb.GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown("w"))
    {
        movement = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
        }
    }
    void FixedUpdate()
    {
            moveCharacter(movement);
    }

    void moveCharacter(Vector3 direction)
    {
        rb.MovePosition(transform.position + (transform.forward * speed * Time.deltaTime));
    }
}

【问题讨论】:

    标签: c# unity3d rigid-bodies


    【解决方案1】:

    在您的代码中,您的 Update 函数中有 moveCharacter 函数,附上的是固定的,现在应该可以工作了。在未调用您的 FixedUpdate 之前,因此您的 moveCharacter 函数效果不佳,并且您的 GameObject 没有移动。

    • 编辑 1:你也应该乘以你的移动方向,更新脚本以适应它
    • 编辑 2:我放错了大括号,现在修复了
    • 编辑 3:您还应该每帧更新您的移动 Vector3,而不是如果按下 W,则再次修复脚本。
    • 编辑 4:修复了移动错误(复制并粘贴整个脚本,因为我更改了更多行)

    这是更新后的脚本:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerTest : MonoBehaviour
    {
        public float speed = 10.0f;
        public Rigidbody rb;
        public Vector3 movement;
    
        // Start is called before the first frame update
        void Start()
        {
            rb.GetComponent<Rigidbody>();
        }
    
        // Update is called once per frame
        void Update()
        {
            movement = new Vector3(Input.GetAxis("Horizontal"), 1f, Input.GetAxis("Vertical"));
        
        }
    
        void FixedUpdate()
        {
            moveCharacter(movement);
        }
    
        void moveCharacter(Vector3 direction)
        {
            Vector3 offset = new Vector3(movement.x * transform.position.x, movement.y * transform.position.y, movement.z * transform.position.z);
            rb.MovePosition(transform.position + (offset * speed * Time.deltaTime));
        }
    }
    

    参考:Rigidbody.MovePosition

    【讨论】:

    • 我现在在工作,所以我无法测试它,但我很想去。谢谢你的帮助!如果您不能详细说明编辑 3,我对此很陌生(显然是大声笑)?我只是好奇如果/为什么最好调用向量而不是仅仅在 if 语句中使用它。
    • 别担心,只要告诉我你是否尝试过,是否有效
    • 第三次编辑:如果你只是在按下 W 时更新矢量,那么你只有在按下 W 时才能移动,移动矢量不会设置为零,因为它只有在 W 时才会更新被按下,所以你的动作将不起作用
    • 刚把它粘贴在那里,它告诉我操作数不能与 2 个向量 3s(粗体部分)一起使用 rb.MovePosition(transform.position + (movement * transform.forward * 速度 * Time.deltaTime));
    • 啊,我认为应该是 movement + transform.forward 尝试将 * 替换为 +
    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多