- Home /
Using Vertical axis in 2D only moves object by 0.1
Hi
I am having some problems with my ship controller. I have want it to move up, down, left and right but when I try to move up or down it only moves by 0.1. Here's my script.
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, moveVertical, 0.0f);
GetComponent<Rigidbody2D>().velocity = movement * speed;
GetComponent<Rigidbody2D>().position = new Vector3
(
Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, boundary.yMin, boundary.yMax)
);
GetComponent<Rigidbody2D>().rotation = -GetComponent<Rigidbody2D>().velocity.x * tilt;
Debug.Log(moveHorizontal);
}
I can not see anything wrong with it.
Thanks in advance
Could you use Debug.Log to post here your maximum moveVertical, moveHorizontal and speed values?
P.S. Also say what you expect it to move by.
@DoTA_$$anonymous$$A$$anonymous$$I$$anonymous$$ADzE I am not really sure what you mean but if I've understood you right, moveVertical gives the same values as moveHorizontal when I put it into the debug.log. It goes from -1 to 1. Just like moveHorizontal
Well your code is fine, I meant this:
In your code your X movement = moveHorizontal*speed; Y movement = moveVertical*speed; Z movement = 0;
So there can be either of 2 problems:
1)Your input Joystick/$$anonymous$$eyboard gives small value. If you use $$anonymous$$eyboard then Unity will simulate for few frames movement of Joystick, if you use Joystick then it gets Joystick position.
Anyway just go configure your axis in Input$$anonymous$$anager (Edit=>Project setting=>Input)
2)Your speed value is small so increase it.
P.S. Lulz, how did I miss it, you use 3D vector for 2D Rigidbody, use 2D vectors everywhere like that:
new Vector2(moveHorizontal, moveVertical);
Or use 3D Rigidbody...
Answer by Dan627 · Sep 12, 2017 at 04:48 PM
You need to use a Vector2 for your position. Change to:
GetComponent().position = new Vector2 ( Mathf.Clamp(rb.position.x,boundary.xMin,boundary.xMax), Mathf.Clamp(rb.position.y,boundary.yMin,boundary.yMax) );
Your answer
Follow this Question
Related Questions
Xbox 360 vertical analog axis not working right? 0 Answers
How to restrict camera rotation to vertical axis? 2 Answers
Vertical axis not working properly 0 Answers
Input Scroll Wheel And Vertical Axis Work Together? 0 Answers
How to detect Double Clicking Horizontal/Vertical Axis on joystick 0 Answers