Movement and speed issues

Started by
6 comments, last by Norman Barrows 6 years, 10 months ago

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”);
}
}

}

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
}

i dont quiet understand how to do it can you please explain a bit more

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.

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

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.

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".

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.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

This topic is closed to new replies.

Advertisement