- Home /
Question by
Squidicus · May 18, 2020 at 06:02 AM ·
scripting problemgravity
My script works almost perfectly. But when I look up or down, I end up going flying down relative to where I'm facing. I know why it's happening, I just cant fix it. Can anyone please help?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour { CharacterController characterController;
public float speed = 10f;
public float gravity = 10f;
public float sensitivity = 1f;
public Vector3 movement;
private void Start()
{
characterController = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float x = Input.GetAxis("Horizontal") * speed;
float z = Input.GetAxis("Vertical") * speed;
float xRotation = Input.GetAxis("Mouse X") * sensitivity;
float yRotation = Input.GetAxis("Mouse Y") * sensitivity;
movement = new Vector3(x, -gravity, z);
transform.Rotate(yRotation, xRotation, 0);
Camera.main.transform.localRotation = Quaternion.Euler(yRotation, xRotation, 0);
movement = transform.rotation * movement;
characterController.Move(movement * Time.deltaTime);
}
}
Comment
never$$anonymous$$d i found out how to fix it and this question hasnt even made it out of moderation :/ but to anyone whos having the same problem all you need to do is keep the x axis with the actual player and then on the camera add the y look
Your answer
![](https://koobas.hobune.stream/wayback/20220612234359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Question on 2d planet gravity 1 Answer
How Do I Increase Gravity Overtime If My Player Stays In The Air Longer? 1 Answer
How to disable player gravity 2 Answers
rigidbody.Use gravity not working from script 0 Answers
Gravity Switch Touch 1 Answer