Sign in to follow this  
NeroGame

Unity Movement and speed issues

Recommended Posts

NeroGame    101

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
cmac    275

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
cmac    275

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
NeroGame    101

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
cmac    275

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
Alberth    9525
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
Norman Barrows    7179

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Affgoo
      About us:
      We are a team of 14 developers developing multiple mid scope games both are over halfway complete. We polish all of our games and focus on quality.


      We are a small team, everyone currently on the team and future teammates must be interested in game development as a whole and not just one role, being a small indie company it is very important that you can wear a few hats and not just one. Everyone on our team is a game dev.

      looking for:
      3d Artist (hand painting a huge plus)
      3d Animator
      entry level Software engineer with reasonable skills in c# / shaders. 

      ^ requirement for all positions: A true love of game development and to be very self motivated.
      We are a very active team, you must be too. 


      If interested or for more information add me on skype: nicholas.boucher4


      Atlas Sentry art style: (art complete)(code complete)
      http://www.slidedb.com/games/atlas-sentry

      Rat n Gat art style: In Devlopment

    • By EvaBalikova
      Main menu in Feudal Alloy. 
      twitter
    • By Jcyshadow97
      Hi guys,i m looking for someone that can work with me on a "top-down" multiplayer fps as 2d and 3d artist.I used photon server and i can take the part of programming.For now i made only the basic gameplay of the game that include shooting,switch weapon and and damage player.If someone can help me please contact me via e mail: 270514974@libero.it.
      I really appreciate your collaboration and hope you have a good day.....
      Thanks for you time to read the post
      At the bottom i attach some screenshot of the current game,i m sorry that i can't attach a video...



    • By Raptor42
      I'm looking to form a new game development team, mostly for training purposes.
      About me:
      I'm a student - Unity C# developer, who worked part-time in this industry for a couple of years already. I've been a lead developer in many "random collab groups" as well as a few companies. I specialize in creating 2D games for Android, but I'm looking forward to trying out new things - especially 3D development.
      Currently, I've got one Android game close to a release so I'd work for this team in my spare time. 
      About the project:
      I've been thinking about creating a simple tycoon-like simulation game for Android (and PC eventually), inspired by the Game Dev Story (initially released by Kairosoft in 1997) https://en.wikipedia.org/wiki/Game_Dev_Story 
      I haven't done much planning though, therefore I'm looking forward to hearing out your ideas.
      Right now, I've only created a test 3D scene using placeholder models and implemented a simple pathfinding system for me to play around with:
      https://i.imgur.com/xAd0l4o.png
      https://i.imgur.com/nHZerOT.png
      I'm looking to work with people who are:
      - willing to take a position of a: 3D modeller/2D artist/Designer
      - not necessarily very experienced, but eager to learn and improve their skills
      - active - check in at least once a day
      If you'd like to apply for a different position which I didn't list here, you are welcome to contact me as well.
      While this project is created mostly for learning purposes, if we ever get to release it and generate any revenue - you will recieve a certain percentage of it.
       
      To Apply:
      Send an email to rk.softwaredev@gmail.com
      Introduce yourself and attach an example of your work (if you have any)
    • By EvaBalikova
      Hello.

      We're a two-member team and I'd like to introduce you our game - Feudal Alloy. 
      It's a metroidvania-style action RPG with fishbowl-powered medieval robots.

      We've been working on the project for a few months. I'd be happy for any feedback.
      teaser
      homepage
      twitter
      facebook
       
      Our Steam page is public now: 
      steam link
       
       



  • Popular Now