【问题标题】:Moving character relative to their location using rigidbody unity使用刚体统一相对于其位置移动角色
【发布时间】:2015-11-05 21:38:11
【问题描述】:

如下面的视频链接所示,播放器相对于全局轴移动,因此会产生一些烦人的效果。

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

这里是正在发生的事情的视频链接: 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);
}

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

【问题讨论】:

    标签: c# unity3d 3d


    【解决方案1】:

    每当您使用变换成员:位置、旋转和有损缩放时,您都在使用世界空间。

    如果您希望使用本地空间/模型空间:您应该使用 localPosition、localRotation 和 localScale。

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

    transform.localPosition+=movement;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多