如何让玩家在扫射时没有“spaz”出来?

时间:2016-08-29 16:38:25

标签: c# unity3d unity5

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

运动代码:

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

1 个答案:

答案 0 :(得分:0)

不要那样处理变换位置。

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

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

Vector3.Up * 6f与新Vector3(0,6,0)相同; 如果你想让你的玩家向上移动,但是他的轮换方式不同,他将不会通过使用新的Vector3()向上移动,使用它的变换来找出指向的位置。

我还建议不要根据相机右/上/左/等移动播放器。