简体   繁体   English

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

[英]Moving character relative to their location using rigidbody unity

As shown in the video link below, the player is moving relative to the global axis, therefore it's having some annoying effects. 如下视频链接所示,播放器相对于全局轴移动,因此具有一些烦人的效果。

I want to make it so the character moves on its local axis, that way it will be easier to apply animations for when he's walking backwards, sideways, etc. How can I do this? 我要使角色沿其局部轴移动,这样,当他向后走,侧身等时,将更易于应用动画。我该怎么做?

Here's the link to the video of what's happening: Link 这是正在发生的事情的视频的链接链接

Also, here is the code for my Character Movement script: 另外,这是我的角色移动脚本的代码:

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

Let me know if you need anything else. 需要帮助请叫我。

Whenever you use the Transform members: position, rotation, and lossyScale, you are working with world-space. 每当使用Transform成员:position,rotation和lossyScale时,您都在使用世界空间。

If you wish to use local-space/model-space: you should instead use localPosition, localRotation, and localScale. 如果您希望使用本地空间/模型空间:您应该改为使用localPosition,localRotation和localScale。

Unfortunately rigidbody does NOT have a Move Local Position function. 不幸的是,刚体没有“移动本地位置”功能。 Instead, try this line to manually update the position: 相反,请尝试以下行以手动更新位置:

transform.localPosition+=movement;

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

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