Assigning input.acceleration to float then adding to a Vector3 to AddForce C#
I have been working to adapt the Roll-a-Ball tutorial to use accelerometer input. I first tried:
float moveHorizontal = Input.acceleration.x;
float moveVertical = -Input.acceleration.y;
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed2);
The issue is if the phone is flat, the ball rolls forward.
Then I tried:
Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, Input.acceleration.y);
rb.AddForce (movement * speed2);
And it works perfect!
Why can't I assign the Input.acceleration to float first, like in my first code snippet?
Mike
Answer by markdavidmorris · Jun 15, 2016 at 05:48 PM
There should be no problem storing the value before creating your movement vector with it. Is this difference due to the fact that you have a negative Input.acceleration.y in the first example, and a positive Input.acceleration.y in the 2nd?
Your answer
Follow this Question
Related Questions
error CS1525: Unexpected symbol `float' 1 Answer
Timer float not evaluating correctly when I check if it's less than another number 3 Answers
Can I compare a Transform variable (x, y, z) against float variables? 1 Answer
float value is reset after every update 1 Answer
currentSpeed doesnt change when pressing Shift (C#) 1 Answer