- Home /
vector2.zero dont work in my code.
Hi, I searched the forums and couldn't find help.
I'm making a flappy bird game, using an similar structure from the tutorial.
I don't understand why when I use the vector2.zero or even if I declare the values as vector2 (0,0) the code simply ignore it.
I tried to put the vector2.zero in the "void update()" to test, but the results we're the same.
Here's the code for the scrolling object:
public class ScrollingObject : MonoBehaviour {
public static ScrollingObject instance;
private Rigidbody2D rb2d;
private float Speed=0;
void Start ()
{
rb2d = GetComponent<Rigidbody2D> ();
instance = this;
}
void Update () {
if (Speed >= GameControler.instance.maxSpeed * -1) {
Speed = Speed + (GameControler.instance.acceleration * -1);
rb2d.velocity = new Vector2 (Speed, 0);
}
}
public void collision()
{
rb2d.velocity = Vector2.zero;
}
}
Thanks in advance.
Answer by kritoa · Jun 06, 2017 at 10:29 PM
You are setting the velocity to zero in the collision function but in the next update you set the velocity according to the old speed (plus acceleration) just like normal. It sounds like what you really want to do is set Speed=0 in the collision function, and then also maybe make a new flag (hasCollided or isDead or gameOver) and set it to true in the collision function, and in your update, check that flag before you do your acceleration.
Thanks a lot for the help, I've already tried to set the speed =0 and to use a flag, the main thing is that I was creating the flag (I supose here you are saying to create a bool) inside the ScrollingObject script, and it didn't work. When I tried to create the same flag on the GameControler script it worked as charm. Another detail is that when the speed is set to 0, the ScrollingObjects remained with the same Speed at the time the collision triggered. In the end, I didn't need the void function collision(), I just needed to set the flag on the player colision. Here's how the code is now:
On the Player Script:
public void OnTriggerEnter2D()
{
GameControler.instance.isDead = true;
}
On the ScrollingObject script:
void Update(){
if (GameControler.instance.isDead != true) {
if (Speed >= Gam
eControler.instance.maxSpeed * -1) {
Speed = Speed + (GameControler.instance.acceleration * -1);
rb2d.velocity = new Vector2 (Speed, 0);
}
} else {
rb2d.velocity = Vector2.zero;
}
}
Thanks again, and sorry I don't have more reputations points to give.
Answer by salamander555 · Jun 04, 2017 at 07:57 AM
I think you don't really understand the voids yet. The problem is that your code just never reach the Vector2.zero line to fix your problem you need to switch the 'collision()' to "OnCollisionStay()" or "OnCollisionEnter()" and it should work
if you want I can explain yu the void function a bit more or you could look up a video about it
Thanks a lot for the answer, Indeed it has been quite some time since I took program$$anonymous$$g classes. I tried to call the function void collision() from the Player script, where I wrote :
public void OnTriggerEnter2D()
{
ScrollingObject.instance.collision ();
}
After read you answer, I tried to call the void function from the scrolling object script, where I tried I use all your sugestions, the way you wrote and adding 2D, tried too OnTriggerEnter2D() and OnTriggerEnter() and OnTriggerEnter2D(Collider2D other) and OnTriggerEnter2D(Collider2D Player).
Nothing seems to work.
I make a manual debug, adding some lines to show me if the void was beeing called, like this: public void collision() { rb2d.velocity = Vector2.zero; playercollision++; playercollisionText.text = "playercollision: "+ playercollision.ToString (); } And it returns every collision so far, but the background and the objects keeps accellerating and never stops.
But, when I add this lines to the void function created in the ScrollingIbject (all the ones that I mentioned above) it only show the number 1 or 2 after every collision.
And thanks for the tip, I'll look for more videos about the void function :)
try this, add a tag to the ScrollingObjects called ScrollingObject, then change your code to this,
void OnTriggerEnter2D(Collider other)// just Collider so it will pick up ANY collider type
{
if (other.tag == "ScrollingObject") {//Check for the tag
other.GetComponent<ScrollingObject>().instance.collision ();
}
}
This is how I always setup my OnTriggerEnter functions and they always work fine for me