Here i have a script that controls the capsule and fps. The fps part works, but the arrows seem to be messing up. The left and right keys don’t move the capsule left and right. Help please?
Thanks. Im new to unity so dont know very much
2626159–184492–FirstPersonController.cs (1.28 KB)
generally it’s better to just paste the script into the forum using the [ code] tags…
If you see someone who needs to know about code tags, please link them to this post: Please use code tags when posting code. You can “tag” your code by typing around your code. There is no overt “Code” tag button that automatically tags a...
Reading time: 9 mins 🕑
Likes: 83 ❤
using UnityEngine;
using System.Collections;
public class FirstPersonController : MonoBehaviour {
public float movementSpeed = 5f;
public float mouseSensitivity = 5f;
public float upDownRange = 60f;
public float jumpSpeed = 6f;
float verticalRotation = 0;
float verticalVelocity = 0;
// Use this for initialization
void Start () {
Screen.lockCursor = true;
}
// Update is called once per frame
void Update () {
CharacterController cc = GetComponent<CharacterController> ();
//Rotating
float rotLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity;
transform.Rotate (0, rotLeftRight, 0);
verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation,0,0);
//Movement
float forwardSpeed = Input.GetAxis ("Horizontal") * movementSpeed;
float sideSpeed = Input.GetAxis ("Vertical")* movementSpeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime;
if (cc.isGrounded && Input.GetButtonDown("Jump"))
{
verticalVelocity = jumpSpeed;
}
Vector3 speed = new Vector3 (sideSpeed, verticalVelocity , forwardSpeed);
speed = transform.rotation * speed;
cc.Move(speed*Time.deltaTime );
}
}
in this case you are depending on the input axis, which means you need to check the arrow keys are set on that axis in the input manager