- Home /
Can't get character to Jump on the Y Axis (C#)
I'm pretty new to Unity and coding, and what I'm trying to do is make an 'expanded version' of the small Roll-A-Ball tutorial game in the Unity Tutorials.
I'm currently trying to make a simple jump function, that moves the ball (The player) up, or along the Y axis. I have made some code which will move the ball, but for some weird reason, it moves the ball along the X axis instead, which is sideways along the floor. This is my C# code:
public class Jumper: MonoBehaviour
{
public float speed;
void Start ()
{
}
void FixedUpdate ()
{
float moveJump = Input.GetAxis("Jump");
Vector2 movement = new Vector2(moveJump, 0.0f);
GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
}
}
I'm using code similar to the perfectly functional one I have which moves the ball normally on the floor, which if you need to know is:
public class PlayerController : MonoBehaviour
{
public float speed;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
}
}
From what I can tell the code is fine, but for some reason 'MoveJump' decides the move along X, instead of the Y axis. Is there a way to change which Axis 'MoveJump' uses? Or do I have to do something completely different instead?
Answer by Lichemperor · Apr 18, 2016 at 06:59 AM
I believe that you need to swap the values of your vector 2 on line 14 around. The first value is X, the second is Y - hence the force being added on the X axis. I don't currently have access to software to test this.
Answer by Ryan_Cori · Apr 21, 2016 at 05:18 PM
What Lichemperer said plus another thing. You are using a Vector2 which technically will work because when converting it into a Vector3 it will just add a value of 0.0 for Z for, but in general for clarity try to have everything be in the same "dimension".
Your answer
Follow this Question
Related Questions
Cant Jump ?c# please 2 Answers
Axis Issue 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
I cant make my character jump ?,Why can't he jump ? 0 Answers
Revise one conflicting code line? 1 Answer