- Home /
Question by
CrazyLule · Oct 27, 2017 at 06:39 PM ·
rotationrotation axis
How to limit how much player can rotate
So Im making FPS game from scratch and I made my player controller , he can move and look but problem is he can rotate all the way around when looking up and down how to limit this ?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float InputKeyX;
public float InputKeyZ;
public float MovSpeed;
public float RotSpeed;
public Vector3 InputX;
public Vector3 InputZ;
public Vector3 _Input;
public float InputMouseX;
public float InputMouseY;
public Vector3 RotationX;
public Vector3 RotationY;
public Vector3 CurrentRotation;
public GameObject Mcamera;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Moving ();
Looking ();
}
void Looking()
{
InputMouseX = Input.GetAxis ("Mouse X");
RotationX = new Vector3 (0,InputMouseX * RotSpeed * Time.deltaTime,0);
transform.Rotate (RotationX);
InputMouseY = Input.GetAxis ("Mouse Y");
RotationY = new Vector3 (InputMouseY * -RotSpeed * Time.deltaTime,0,0);
Mcamera.transform.Rotate (RotationY);
}
void Moving()
{
InputKeyX = Input.GetAxis ("Horizontal");
InputKeyZ = Input.GetAxis ("Vertical");
InputX = new Vector3 (InputKeyX * MovSpeed * Time.deltaTime, 0, 0);
InputZ = new Vector3 (0,0,InputKeyZ * MovSpeed * Time.deltaTime);
_Input = InputX + InputZ;
transform.Translate (_Input);
}
}
Comment