- Home /
Why does this script make my camera jump when I move back?
Alright, so I got a script from somewhere that I tweaked a bit, and it makes my character jump when I hit S, or Down while looking at the ground. Can someone please help me fix this bug?
using UnityEngine;
using System.Collections;
public class fps_move_player : MonoBehaviour {
public float speed = 50.0F;
public float jumpSpeed = 60.0F;
public float gravity = 60.0F;
private Vector3 moveDirection = Vector3.zero;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CharacterController controller = GetComponent<CharacterController>();
// is the controller on the ground?
if (controller.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.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
What script are you using for the camera?
You should cache the controller variable if it always going to be the same object/component.
CharacterController controller;
void Start () {
controller = GetComponent<CharacterController>();
}
Answer by asdf123 · Aug 09, 2013 at 01:39 AM
line 22:
try this: moveDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"),0 );
instead of: moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Sorry, but this answer is incorrect. The character is moving on the X-Z plane, therefore the moveDirection calculation in the question is correct.
The problem would be the camera script, which is not given in the question.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093324im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Jumping increases movement speed 1 Answer
Player object falling slowly when holding input 2 Answers
Input.GetAxis won't start working properly. 1 Answer
Movement FPS Independent, how to? 1 Answer