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

时间:2015-11-04 11:18:47

标签: c# unity3d 3d

如下面的视频链接所示,播放器相对于全局轴移动,因此它有一些令人讨厌的效果。

我想让角色在本地轴上移动,这样就可以更容易地应用动画,因为它可以在他向后,侧身等行走时使用动画。我该怎么做?

以下是视频的链接: 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);
}

如果您还有其他需要,请告诉我。

1 个答案:

答案 0 :(得分:0)

无论何时使用Transform成员:position,rotation和lossyScale,您都在使用world-space。

如果你想使用局部空间/模型空间:你应该使用localPosition,localRotation和localScale。

不幸的是,刚体没有移动本地位置功能。 相反,请尝试使用此行手动更新位置:

SELECT r.Role_Name
       FROM tbl_Roles r
       WHERE NOT EXISTS(
             SELECT 1 
             FROM tbl_Users_Roles_MTM ur
             WHERE ur.User_ID = @User_ID
             AND   ur.Role_ID = r.Role_ID);