- Home /
AddForce transform.right not working correctly in 2d
Why does the rigidbody2D addforce work when using transform.up perfectly but using transform.right doesn't work correctly. It seems to just transform the players position.
public Transform fireDirection;
public float speed = 1f;
Transform collisionLinePoint1;
Transform collisionLinePoint2;
public int health = 100;
Animator alienAnime;
Rigidbody2D rb;
bool colliding;
GameObject player;
public Player_Control playerControl;
Rigidbody2D playerRb;
void Awake()
{
player = GameObject.FindWithTag ("Player");
collisionLinePoint1 = transform.FindChild ("collision_S");
collisionLinePoint2 = transform.FindChild ("collision_E");
collisionLinePoint1.localPosition = new Vector3 (1.15f, 1.82f, 0f);
collisionLinePoint2.localPosition = new Vector3 (1.15f, -1.94f, 0f);
}
// Use this for initialization
void Start () {
playerRb = player.GetComponent<Rigidbody2D> ();
alienAnime = GetComponent<Animator> ();
rb = GetComponent<Rigidbody2D>();
player.GetComponent<Rigidbody2D> ();
}
void Update () {
Death ();
if (Input.GetButtonDown ("Fire3")) {
// Doesnt work correct???
playerRb.AddForce (transform.right * 400);
// This works fine
playerRb.AddForce (transform.up * 400);
}
}
To help you we need more information. What does it NOT do? What does it do? What do you mean by "doesn't work correct". Etc. UA useres will be able to help you more if you answer one of these questions.
Ok. When playerRb.AddForce (transform.up 400); is used you can see addForce is applying force using physics like it should. When I use playerRb.AddForce (transform.right 400); its doesn't appear to use force, it just transforms the players position to another location in one frame.
Answer by Guppie1337 · Jun 09, 2015 at 07:39 PM
One reason this could be an issue is because you're condition is only met once on the frame the button is pressed. If you're expecting the object to move immediately and gain the expected velocity, you can AddForce with ForceMode.Impulse.
//Only returns true for the frame it was pressed.
if (Input.GetButtonDown ("Fire3"))
playerRb.AddForce (Vector2.right * 400, ForceMode2D.Impulse);
Keep in mind that transform.right and Vector2.right are essentially the same thing (1, 0). So by multiplying that by 400 you're getting (400, 0) every frame. Unless you've adjusted all other physics properties, this is gonna send your object flying with default values. Hope this helps.
Answer by The_Guy · Jun 08, 2015 at 07:44 AM
I think you mean to be using
playerRb.AddForce (Vector2.right * 400);
and also you could make 400 a public variable to edit it in the inspector on the fly.
What are you trying to move and how much mass do you have set on the rigidbody? Collider materials automatically come with a friction property of 1, so there might be too much friction and 400 wont push him enough. Try a ridiculous number like 50000 and see if that pushes him. Also I think you want to try keeping rigidbody function inside of FixedUpdate() ins$$anonymous$$d of just Update().
Yea I can see what's happening now. it does move more but still looks like its transporting to the position rather that physics moving him. I think I have mistaken what addforce is. I changed the if (Input.GetButtonDown ("Fire3")) to (Input.GetButton ("Fire3")) and I can see it works correctly when I hold down "Fire3". Is there anything else available to add sudden force to a object in one frame in 2D like Explosive force?
To achieve the sudden force you would want to specify Impulse in an AddForce call. So it would look like this: myRigidbody2D.Addforce(Vector2.right, Force$$anonymous$$ode2D.Impulse);