- Home /
Cannot limit velocity when using tilt acceleration
Hey guys,
I am using following script to move my Player by tilting the phone:
void FixedUpdate ()
{
tiltAcceleration = Vector3.zero;
tiltAcceleration.x = Input.acceleration.x;
if (tiltAcceleration.sqrMagnitude > 1)
tiltAcceleration.Normalize();
tiltAcceleration *= Time.deltaTime;
transform.Translate (tiltAcceleration * sideSpeed);
// This is how I try to limit my velocity of Player, but it DOES NOT WORK!
playerRigidbody.velocity = Vector3.ClampMagnitude (playerRigidbody.velocity, 5f);
}
In the code above, you can see at the last line, how I am tryint to limit my velocity while tilting the phone - however, this does not work, it still moves too fast, so it passes the Box Colliders (side-walls).
I have read few answers here on Unity, but noone helped in my case.
Any advice appreciated.
Answer by doublemax · Jan 19, 2017 at 09:45 AM
As you're setting the position of the object yourself ( with transform.Translate ), the velocity property is not used. What you need to limit is the value of (tiltAcceleration * sideSpeed)
However, if you're using physics - and it looks that you are - you shouldn't translate an object directly, instead use AddForce() to apply a force in the direction of the tilt.
$$anonymous$$akes sense, I will test it! But Unity is crashing on me alot today... need to check whether I have too much assets important into project or what could the reason be.
Your answer
Follow this Question
Related Questions
swap between 3 gameobjects mid game 0 Answers
gradually moving an object up to speed rather then instantly? 7 Answers
3d animation controller 0 Answers
3d Sound not working? 0 Answers
DontDestroyOnLoad() Unity 3D 0 Answers