Jump to content
  • Advertisement
Sign in to follow this  
NeroGame

Unity Movement and speed issues

This topic is 470 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello everyone !
i’m making a 2d game with unity  basically 4 objects are supposed to float in the play field with their speed increase over time i tried many scripts to get my movement as i wanted it to be but nothing seems to work i just want my objects to move and float in the play field and bounce when the collide with each other or with the walls also so speed increase is not working as time goes on the objects slow down and move very slowley and thats not what i want , i need my objects to keep moving and to gain speed as time goes on so i need help please here is my script

 

 

using UnityEngine;

public class Movement : MonoBehaviour
{
//speed of the ball
public float speed = 3f;
public float intervale = 5f;
public float accel = 2f;
public double passedt ;
//the initial direction of the ball
private Vector2 spawnDir;

//ball’s components
Rigidbody2D rig2D;
// Use this for initialization
void Start()
{
//setting balls Rigidbody 2D
rig2D = this.gameObject.GetComponent<Rigidbody2D>();

//generating random number based on possible initial directions
int rand = Random.Range(1, 4);

//setting initial direction
if (rand == 1)
{
spawnDir = new Vector2(1, 1);
}
else if (rand == 2)
{
spawnDir = new Vector2(1, -1);
}
else if (rand == 3)
{
spawnDir = new Vector2(-1, -1);
}
else if (rand == 4)
{
spawnDir = new Vector2(-1, 1);
}

//moving ball in initial direction and adding speed
rig2D.velocity = (spawnDir * speed);

}

private void FixedUpdate()
{
passedt = double.Parse(Timer.secondes);
if (passedt == intervale)
{
speed += accel * Time.deltaTime;
Vector2 veloc = new Vector2(speed, 0);
rig2D.velocity = veloc;
intervale += 5;
Debug.Log(“test”);
}
}

}

 

Share this post


Link to post
Share on other sites
Advertisement

You have an if statement that compares equality in floats. Don't do this. If you do need to check equality, you should use Mathf.Approximately.

In this case it looks like you're just checking if the timer has exceeded an interval. Check if the elapsed time is greater than the interval.

And instead of adding the base interval value to itself each interval, just reset the elapsed time.

if (timer > interval)
{
    timer = timer - interval;
    // whatever else happens each interval
}
Edited by cmac

Share this post


Link to post
Share on other sites

This line is a problem: 

if (passedt == intervale)

That's all I'll say, figuring out your own problems is integral to learning as a programmer.

Share this post


Link to post
Share on other sites

i think i didnt explain very well i checked equality because i want my speed to increase every 5 secondes that why i checked if the passed time is equal to interval and then i added 5 to the interval so it can keep track of the timer

Share this post


Link to post
Share on other sites

Yes, and that won't work. Think about why == on an timer float isn't working.

Even if you didn't know this was the issue, you can print debug logs in appropriate if statements that tell you what code is being executed, eg. Debug.Log("Timer interval passed, increasing speed") inside your if statement would never fire, which would indicate where the problem is.

Edited by cmac

Share this post


Link to post
Share on other sites
i dont quiet understand how to do it can you please explain a bit more

Basically, floating point numbers are approximations, they are always off from the "real" number by some small amount.

Thus if you test for one specific value, it is about 99% chance you'll get "false", even if the value is supposed to be the value you're testing for.

 

It's comparable with an assignment to throw a coin say 1 meter exactly. Even if we manage to agree on how we measure the distance, you will never succeed, as it's impossible to throw a coin 1,000000000000 meter (that's 1m accurate to 1nm).

With any float computation, the same happens, it's 1m, but not to 1nm accuracy. The cause is how computer systems store and perform floating point operations, so this problem happens with every computer system, in all computer languages.

The solution is to never test for a single value, as it will always return false. Instead, test for a range (Mathf.Approximately apparently, never used it), or test for a lower or upper bound, ie  value >= 1.0 works for all distances that are at least 1m.

 

For more information, search the interwebs for "what every programmer should know about floating point".

Edited by Alberth

Share this post


Link to post
Share on other sites

intervale is a float and passedt is a double. so its doing either a 32 or 64 bit cmp (comparison) of the two values. its HIGHLY unlikely that you just happen to call Parse(Timer.secondes) when the result is EXACTLY equal to interval. 

example:

fixedUpdate runs and gets a passedt of 4.999999....

fixedUpdate runs the next time and gets a passedt of 5.00000000000001

right there you've missed the 5 second point.

it needs to be >=

 

one possible general algo for timers is:

1. start time = get_time()

2. repeat: ET = get_time() - start time. until ET>= desired ET.

ET is the same as your passedt and desired ET is the same as your intervale.

Edited by Norman Barrows

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!