- Home /
Moving camera up/down causes player to drift upwards,Up/down movement when moving camera on x axis [SOLVED]
I was working on a basic FPS controller, but i found that whenever i move my camera up/down my character will gradually move up. is there something I'm doing wrong or will I have to redo my code again? Sorry if the solution is really simple, I've only been using Unity for 5 days.
This is my code: using System.Collections; using System.Collections.Generic; using UnityEngine; //CharacterController based FPS movement/camera script. public class ControlFPS : MonoBehaviour { //Adjustable variables public float speed = 2f; public float sensitivity = 2f; public float gravity = 1f; public Vector2 XaxisMinMax = new Vector2(50, -50); public GameObject eyes; //player CharacterController player; //movement variables float horimove; float vertmove; //Camera Y/X axis, and Player capsule's Y axis orientation float Yaxis; float Xaxis; float RotY; //Thing I use for gravity Vector3 moveDirection = Vector3.zero; //Program recognizes charactercontroller as player void Start() { player = GetComponent<CharacterController>(); } //code to collect input and rotate the player on Y axis void FixedUpdate() { //WASD movement horimove = Input.GetAxis("Horizontal") * speed * Time.deltaTime; vertmove = Input.GetAxis("Vertical") * speed * Time.deltaTime; //Camera and Capsule rotation on Y axis Yaxis += Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime; RotY = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime; //Camera rotation on X axis and limit to rotation Xaxis -= Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime; Xaxis = Mathf.Clamp(Xaxis, XaxisMinMax.y, XaxisMinMax.x); //Gravity if (!player.isGrounded) { moveDirection.y -= gravity * Time.deltaTime; player.Move(moveDirection * Time.deltaTime); } //rotate player on Y axis transform.Rotate(0f, RotY, 0f); } //code to rotate the camera and move the player void LateUpdate() { //rotate camera eyes.transform.eulerAngles = new Vector3(Xaxis, Yaxis, 0f); //move the player Vector3 movement = new Vector3(horimove, 0, vertmove); movement = transform.rotation * movement; player.Move(movement); } }
Edit: Solved the issue, turns out the rigidbody component on my player capsule was causing the problem.
Your answer
Follow this Question
Related Questions
[ C#] Character Movement based on Camera Direction. 2 Answers
How do I make my character lean to the sides? 0 Answers
How To Desync A Player's Camera? 1 Answer
Roll/Dodge/Evade System 1 Answer