Question by
migueeel · Jun 13, 2017 at 05:29 PM ·
camerafpsmovement script
FPS movement script moves up/down when aiming up/down?
(couldn't think of a good title)
So, I'm trying to make a FPS script for a LAND character, that shouldn't fly, but when I look straight to the ground or to the air, my char moves in the Y axis instead of "forward". I guess the problem is that the transform is rotating and thus forward becomes up or down, but I don't know how to stop that.
Movement Code:
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move(h, v);
//Turning();
Animation(h, v);
}
void Move(float h, float v)
{
moveforth = transform.forward * v;
moveside = transform.right * h;
movement = moveforth + moveside;
movement = movement.normalized * speed * Time.deltaTime;
rb.MovePosition(transform.position +movement);
}
Camera Code:
public class MouselookCopypasta : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
Quaternion originalRotation;
void Start()
{
Rigidbody rigidbody = GetComponent<Rigidbody>();
// Make the rigid body not change rotation
if (rigidbody)
{
rigidbody.freezeRotation = true;
}
originalRotation = transform.localRotation;
}
void Update()
{
if (axes == RotationAxes.MouseXAndY)
{
// Read the mouse input axis
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = ClampAngle(rotationX, minimumX, maximumX);
rotationY = ClampAngle(rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, -Vector3.right);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
{
angle += 360F;
}
if (angle > 360F)
{
angle -= 360F;
}
return Mathf.Clamp(angle, min, max);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612124108im_/https://answers.unity.com/themes/thub/images/avi.jpg)