- Home /
How to fix player jumping while watching downwards and moving with s key ?
Hello, I am making a game in unity and I have used unity's new input system and I am facing a problem that whenever I look down and move by pressing s (back) or whenever I look up and move using w (forward) player jumps in both cases. Any suggestions ?? Thanks in advance.
please share the relevant code parts as well as a screenshot of you input system setup so that people can actually help you.
Answer by stalwart5033 · Mar 10 at 05:48 PM
Probably your player trying move into air, you can set your camera to child of moving parent body, and adapt controller scripts to the parent transform. Probably camera changing your moving object's rotation and player trying to move in the direction to you're looking because of "transform.forward". Transform.forward always calculates orientation of transforms forward even if you look at the air
Thanks for you reply. Please give me some time and I will update accordingly.
Thanks it worked!. But still whenever I look very top and to very bottom and move the player speed decreases.
I didn't make it very detailed but I write a simple fps player controller script, you can add your own camera controls to camera related lines, and the camera must be the child of the moving object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
private CharacterController controller;
private Vector3 playerVelocity;
private bool groundedPlayer;
public float playerSpeed = 2.0f;
private float jumpHeight = 1.0f;
private float gravityValue = -9.81f;
public Transform Camera;
public float sens = 2f;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
// Debug.Log(transform.forward);
Vector3 forward = transform.forward * Input.GetAxis("Vertical");
Vector3 sideward = transform.right * Input.GetAxis("Horizontal");
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
controller.Move((forward + sideward) * Time.deltaTime * playerSpeed);
Vector3 cameraHorizontalAxis = Camera.localEulerAngles;
Vector3 cameraVerticalAxis = transform.localEulerAngles;
cameraHorizontalAxis.x -= Input.GetAxis("Mouse Y") * sens;
cameraVerticalAxis.y += Input.GetAxis("Mouse X") * sens;
transform.localRotation = Quaternion.AngleAxis(cameraVerticalAxis.y, Vector3.up);
Camera.localRotation = Quaternion.AngleAxis(cameraHorizontalAxis.x, Vector3.right);
// Changes the height position of the player..
if (Input.GetButtonDown("Jump") && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
Terrain Slope Bouncing 1 Answer
Using Navmesh for Player navigation 1 Answer
Moving forward towards the cursor with LookAtMouse? 0 Answers