【问题标题】:How do I make a player not "spaz" out when strafing?如何让玩家在扫射时不会“发呆”?
【发布时间】:2016-08-30 01:01:04
【问题描述】:

所以团结一致,我一直在努力使这个第三人称控制器脚本工作,到目前为止它按我希望的方式工作,只是现在我被困在如何让他前进并离开同时(或类似的其他方向)因为现在他刚刚开始窃听。

动作代码:

using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class Character_Controler : MonoBehaviour
{
    [SerializeField]
    public float speed = 1f;
    // the world-relative desired move direction, calculated from the camForward and user input.
    private bool canJump = false;
    Vector3 movement;
    Rigidbody playerRbody;

        void Awake()
        {
            playerRbody = GetComponent<Rigidbody>();

        }
    private void FixedUpdate()
    {
        playerRbody.AddForce(Physics.gravity * playerRbody.mass);
       // float h = Input.GetAxisRaw("Horizontal");
       // float v = Input.GetAxisRaw("Vertical");
        var jump = Input.GetKeyDown(KeyCode.Space);
        if (!canJump && playerRbody.velocity.y == 0)
        {
            canJump = true;

        }
        if (jump && canJump == true)
        {
            playerRbody.AddForce(new Vector3(0, 6f, 0), ForceMode.Impulse);
            canJump = false;
        }
         if (Input.GetKey(KeyCode.W))
         {
             moveForward(speed);
         }
         else if (Input.GetKey(KeyCode.S))
         {
             moveBack(speed);
         }

         if (Input.GetKey(KeyCode.A))
         {
             moveLeft(speed);
         }
         else if (Input.GetKey(KeyCode.D))
         {
             moveRight(speed);
         }
        transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, transform.localEulerAngles.z);
        //transform.forward = Camera.main.transform.forward;

    }

    }
    private void moveForward(float speed)
    {
        transform.forward = Camera.main.transform.forward;
        // Quaternion newRotation = Quaternion.LookRotation(transf);
        // playerRbody.MoveRotation(newRotation);
        transform.localPosition += transform.forward * speed * Time.deltaTime;
    }

    private void moveBack(float speed)
    {
        transform.forward = -Camera.main.transform.forward;
        transform.localPosition += transform.forward * speed * Time.deltaTime;

    }

    private void moveRight(float speed)
    {
        transform.forward = Camera.main.transform.right;
        transform.localPosition += transform.forward * speed * Time.deltaTime;
    }

    private void moveLeft(float speed)
    {
        transform.forward = -Camera.main.transform.right;
        transform.localPosition += transform.forward * speed * Time.deltaTime;
    }
}

【问题讨论】:

  • 如果你的玩家有一个刚体,你为什么要直接修改变换的位置?此外,您应该只在一个功能中完成所有动作,而不是 4。
  • @XanderLuciano 这是我让播放器按我想要的方式工作的唯一方法,我会稍微详细解释一下,必须去 abit 。

标签: c# unity3d unity5


【解决方案1】:

不要这样处理变换位置。

https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

https://docs.unity3d.com/ScriptReference/Rigidbody.html

而且 Vector3.Up * 6f 与 new Vector3(0, 6, 0); 相同 如果你想让你的玩家向上移动,但他的旋转方式不同,他不会通过使用 new Vector3() 相对于他的旋转向上移动,使用它的变换来找出 up 指向的位置。

我还建议不要将基于相机的播放器向右/向上/向左/等移动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 2021-04-11
    相关资源
    最近更新 更多