Can't jump when moving backwards and to the right or left (C#)
I've been working on this 3d shooter project for quite a while now, but a major problem that has always stumped me was the issue that I could not jump when moving backwards and right (or backwards and left). I can jump just fine any other direction, just not those specific directions. Any help is appreciated: Thanks!
Here's the code:
using UnityEngine;
using System.Collections;
public class FPSControllerNew : MonoBehaviour {
//Variables
public float speed = 5.0F;
public float jumpSpeed = 12.0F;
public float gravity = 20.0F;
public float sensitivity = 2f;
public bool isCrouching = false;
private Vector3 moveDirection = Vector3.zero;
public GameObject camera;
public GameObject upperbody;
public GameObject WeaponInHand;
public Collider playerCollider;
public Renderer playerRenderer;
float rotX;
float rotY;
Transform cameraTransform;
void Start ()
{
cameraTransform = Camera.main.transform;
}
void FixedUpdate() {
CharacterController player = GetComponent<CharacterController>();
player.transform.Rotate (0, rotX, 0);
// is the player on the ground?
if (player.isGrounded) {
//Feed moveDirection with input.
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
//Multiply it by speed.
moveDirection *= speed;
}
//Jumping
if (Input.GetKey(KeyCode.Space) && player.isGrounded)
{
Debug.Log("Jumping");
moveDirection.y = jumpSpeed;
}
//Applying gravity to the player
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
player.Move(moveDirection * Time.deltaTime);
Having pasted your code and used it, it works for me with the data you assigned to those variables. There a lot of weird things you are doing that I don't, I guess.
player.transform.Rotate (0, rotX, 0); // not sure what this is doing
CharacterController player = GetComponent<CharacterController>(); // Should store this in awake, ins$$anonymous$$d of getting it every fixed update.
Also with Character controllers, you can use Update (ins$$anonymous$$d of FixedUpdate). It doesn't use physics in the standard way.
$$anonymous$$y conclusion is something else is affecting the object that isn't this.
Answer by Mochna · Dec 24, 2018 at 04:43 AM
Well, the answer to this was actually really weird; I don't know how I didn't notice this before, but for some reason my keyboard does not record any input on the space key when holding s and a or d. I have no reason to why that happens, but it just does; it has no connection with the project itself. When switching to another keyboard, it worked perfectly, so in short, there was nothing really wrong about the script.
Your answer
![](https://koobas.hobune.stream/wayback/20220612184502im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with Vector3.Distance 0 Answers
Fast start and ending at jump but slow middle. 0 Answers
How to make the character walk-jump and walk (and resize the collider when crouched) ? 0 Answers
Anyone have any ideas on how I could improve my character jump to make it more realistic? 1 Answer
How can I make it so that the longer you hold down the jump button the higher the player jumps? 0 Answers