Advertisement

Change speed of object

Started by May 12, 2018 03:37 AM
14 comments, last by Scouting Ninja 6 years, 4 months ago

Awesome I'll check this out!

 

So I updated the collision function, but I'm still not getting the desired behavior. One, it's not launching till the end of the screen. It goes to a point and eventually starts falling again, like it's run out of momentum, similar to if you throw a baseball in the air. I want it to keep going at a constant speed until it hits something else. Also, it isn't going back to it's normal speed after the timer runs out and changes the speed variable back to 1. It also seems to be significantly faster from the start, without touching any power ups. I also just noticed that if the timer is up and it hits something, it bounces off with more speed but then slows back down to where it can't keep itself going and eventually just lands back on the paddle or through the bottom of the screen.

I think the reason has to do with the PowerUpTimer method. I put a breakpoint at the start of it and it never hit it. However, if I put one on the call for the method, it breaks there, but it doesn't seem to actually access the method? Which I'm pretty confused about.

Is there something wrong with this code:


public IEnumerator PowerUpTimer (int delay)
	{
		yield return new WaitForSeconds(delay);
		Speed = 1;
		this.gameObject.GetComponent<Rigidbody2D>().velocity = this.gameObject.GetComponent<Rigidbody2D>().velocity.normalized;
		this.gameObject.GetComponent<Rigidbody2D>().velocity *= Speed*MaxSpeed;

	}

I tried placing a break point on it, but it shows up with a white dot in the center instead of the normal white swirly or whatever. I'm not sure what it means, but I'm sure that it is not breaking there. I tried it on every line of the method and I get the same results, but if I do it on a different method it works fine.

Advertisement

When I get home I will try to make a basic power up and will link it here. You can then compare it with your own code and see how it works.

This problem is the same as before; the parts you are showing doesn't look wrong. Meaning the problem could be something else.

 

Actually I just figured it out a few minutes ago. Apparently the code I just posted can't be public. I had to set it to private and use a method in the same class to call StartCoroutine(PowerUpTimer(delay)). I guess there is something about Coroutines having issues being public? I found a few questions about it online and this was the recommended solution. I've now got a working fast and slow ball pick up!

9 minutes ago, ethancodes said:

Actually I just figured it out a few minutes ago.

That is good ;), Coroutines and Unity does have weird problems from time to time.

Often it's easier and more stable to setup a timer.


class Timer(){
  public float TimeLeft = 0f;
  
  public bool UpdateTimer(){
    if TimeLeft > 0f
    {
      TimeLeft -= Time.deltaTime;//Delta time works in seconds
    }else{
      return true;
    }
    return false;
  }
}

Coroutines are special in that they can use multithreading, so they could have limitations to make them safe.

This topic is closed to new replies.

Advertisement