简体   繁体   English

在 Unity 和 C# 中沿其面对的方向移动刚体

[英]Moving a rigid body in the direction it's facing in Unity and C#

I've been trying to get this working using pretty much every method I can find (even using the dreaded transform.translate) but I just can't seem to get it working.我一直在尝试使用我能找到的几乎所有方法(甚至使用可怕的 transform.translate)来让它工作,但我似乎无法让它工作。 This is just a draft of the code and if there are any other ways to go about it I'm down to change some stuff.这只是代码的草稿,如果还有其他方法可以解决 go 的相关问题,我将更改一些内容。

Currently, he barely moves (it looks like he's getting stuck on the floor somehow.) I'm fairly new to moving objects using rigid bodies so I'm pretty much in the dark on how to solve this issue.目前,他几乎没有移动(看起来他不知何故被卡在了地板上。)我对使用刚体移动物体还很陌生,所以我对如何解决这个问题几乎一无所知。

Here's the script from my latest crack at it:这是我最新破解的脚本:

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

In your code you have your moveCharacter function inside your Update function, enclosed is the fixed one, which should work now.在您的代码中,您的更新 function 中有您的 moveCharacter function,随附的是固定的,现在应该可以使用。 Before your FixedUpdate was not getting called, therefore your moveCharacter function was not as well and your GameObject was not moving.在您的 FixedUpdate 没有被调用之前,因此您的 moveCharacter function 效果不佳,并且您的游戏对象没有移动。

  • EDIT 1: You shoud also multiply with your movement direction as well, updated the script to fit that编辑1:你也应该乘以你的移动方向,更新脚本以适应它
  • EDIT 2: I misplaced the curly brackets, fixed that now编辑2:我放错了大括号,现在修复了
  • EDIT 3: You should also update your movement Vector3 every frame, not if W is pressed, fixed the script again.编辑3:您还应该每帧更新您的移动Vector3,而不是如果按下W,则再次修复脚本。
  • EDIT 4: Fixed the movement bug (copy and paste entire script, since i changed more lines)编辑 4:修复了移动错误(复制并粘贴整个脚本,因为我更改了更多行)

This is the updated script:这是更新的脚本:

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

References: Rigidbody.MovePosition参考: 刚体.MovePosition

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM