- Home /
Rigidbody2D speed boost when entering a trigger
I am making a 2D game and I want a 2D rigid body to get a speed boost when entering a 2D trigger, but I cannot get it to work. The object is not getting a speed boost. I am new to coding and am coding in C#. Here is the script I have.
void OnTriggerEnter2D(Collider2D other)
{
rigidbody2D.AddForce(Vector2(10, 0));
}
Is this code even triggering?
Is it attached to the player, not the pickup?
$$anonymous$$aybe 10 is not strong enough
Change the Forcemode for different results
Are you changing the rigidbody's velocity somewhere (Update, FixedUpdate)?
I have the script attached to the trigger not the player. I want the object with the tag "Player" to be pushed in the positive x direction upon entering the trigger zone. I have changed the script to only affect the player object and increased the force to 100. I am not getting any errors but the object still does not get a boost in speed when it enters the trigger zone. I am not changing the velocity of the rigidbody anywhere. This is the entire script that is attached to the trigger now.
using UnityEngine;
using System.Collections;
public class Boost : $$anonymous$$onoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
rigidbody2D.AddForce(Vector2(100, 0));
}
}
}
Answer by hexagonius · Mar 02, 2015 at 08:20 PM
If you just call rigidbody2D then you're referring to the triggers. Put other. In front of it
Answer by 10XL · Sep 15, 2015 at 10:56 PM
You need to access Rigidbody2D of Collider2D other that is entering the trigger. Keyword new is required for constructors. void OnTriggerEnter2d (Collider2D other) { other.GetComponent<Rigidbody2D> ().AddForce (new Vector2(10f, 0f)); }
Answer by Jeason1997 · Sep 16, 2015 at 03:37 PM
If you use Unity5, you can use the "Area Efctor 2D" compoent to make this