使用刚体统一性相对于其位置移动角色

Moving character relative to their location using rigidbody unity

如下面的视频 link 所示,播放器相对于全局轴移动,因此产生了一些令人讨厌的效果。

我想让角色在其局部轴上移动,这样当他向后、侧身等行走时应用动画会更容易。我该怎么做?

这里是 link 视频中发生的事情: Link

此外,这是我的角色移动脚本的代码:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 6f;            // The speed that the player will move at.
    Vector3 movement;                   // The vector to store the direction of the player's movement.
    Animator anim;                      // Reference to the animator component.
    Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
    int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
    float camRayLength = 100f;          // The length of the ray from the camera into the scene.

void Awake ()
{
    // Create a layer mask for the floor layer.
    floorMask = LayerMask.GetMask ("Floor");

    // Set up references.
    anim = GetComponent <Animator> ();
    playerRigidbody = GetComponent <Rigidbody> ();
}


void FixedUpdate ()
{
    // Store the input axes.
    float h = Input.GetAxisRaw ("Horizontal");
    float v = Input.GetAxisRaw ("Vertical");

    // Move the player around the scene.
    Move (h, v);

    // Turn the player to face the mouse cursor.
    Turning ();

    // Animate the player.
    Animating (h, v);
}

void Move (float h, float v)
{
    // Set the movement vector based on the axis input.
    movement.Set (h, 0f, v);

    // Normalise the movement vector and make it proportional to the speed per second.
    movement = movement.normalized * speed * Time.deltaTime;

    // Move the player to it's current position plus the movement.
    playerRigidbody.MovePosition (transform.position + movement);
}

如果您还需要什么,请告诉我。

只要您使用变换成员:position、rotation 和 lossyScale,您就在使用 world-space。

如果您想使用 local-space/model-space:您应该改用 localPosition、localRotation 和 localScale。

不幸的是,刚体没有 MoveLocalPosition 函数。 相反,请尝试使用此行手动更新位置:

transform.localPosition+=movement;