Dear All,
I’m making an FPS game for practice. I managed to get the look and the movement done so far but I have encountered an issue:
There is only one direction as “forward” in the game so when I turn around with the mouse, the character still runs to the old direction as forward. It does not get updated and it is impossible to navigate.
I’m using the MouseLook script and my own movement scripts for the job.
My character is the following:
I’m having a playerCharacter gameobject that has a rigidbody and a capsule collider on it. one if its children is the MainCamera, and the camera’s child is the arms and the weapon.
I have the mouselook script on the camera (Y) and the playerCharacter(X). The character movement script is on the playerCharacter, animations on the arms but that’s not important at the moment.
my scripts:
using UnityEngine;
using System.Collections;
public class CharacterMovement : MonoBehaviour {
private Rigidbody playerRigidbody;
private Vector3 movement;
private float speed = 6.0f;
void Awake()
{
playerRigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate ()
{
float movementInputHori = Input.GetAxisRaw("Horizontal"); //getting w,a,s,d
float movementInputVerti = Input.GetAxisRaw("Vertical");
bool fireInput = Input.GetButton("Fire1"); //if someone is holding down mouse1, it remains true. else false.
Move(movementInputHori, movementInputVerti);
}
void Move(float movementInputHori, float movementInputVerti)
{
movement.Set(movementInputHori, 0.0f, movementInputVerti);
playerRigidbody.velocity = movement.normalized * speed * -1;
}
}
------------------------------------
using UnityEngine;
using System.Collections;
public class MouseLook : 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 rotationY = 0F;
void Start()
{
if (GetComponent<Rigidbody>()) GetComponent<Rigidbody>().freezeRotation = true;
}
void Update()
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}