- Home /
rigidbody.velocity not working on x axis
I have this code that pushes the player when they collide with the object its attached to either up down left or right depending on the integer. It works fine for the y axis, but it doesn't move the player on the x-axis
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
other.rigidbody.velocity = new Vector2(xPower, yPower);
}
}
if yPower = 10 or -10 and xPower = 0. It will push the player either up or down. But if xPower = 10 or -10, and yPower = 0 it will not push the player Left/Right. Does anybody know why?
other.rigidbody.velocity = new Vector3(xPower, yPower, other.rigidbody.velocity.z);
Try that.
Why not just use a physics material and it'l bounce away depending on collision
Answer by VoidChimera · Jan 02, 2015 at 06:23 AM
A potential issue might be that you are using a normal rigidbody with a 2D player, rigidbody2D is probs better in this case, seeing as you are using 2D stuff. Other then that, have you tried using Debug.Log to see if your code is being run correctly? Also, you might wanna provide the code for the calculation for xPower, as your problem might actually be there
Answer by kerelolivier · Dec 11, 2016 at 12:34 AM
If you constrained the x position on the rigid body, velocity does not apply to the x-axis.