I tried looking it up and was unable to find an answer, tons of other people asked basically the same question but not of those answers seemed to work. My code is:
GetComponent ().velocity = new Vector2 (velocity, 0);
Right now it just makes it move right no matter what way it’s pointing. I’m trying to launch a physics object so I can’t just do transform.position. Any ideas?
I managed to figure out an easy simple solution, with easy small amounts of code.
Simply add this into your script.
GetComponent ().AddForce (transform.right * velocity);
Then create a public float named velocity, and you should be good to go. To explain it for anyone trying to learn, GetComponent().AddForce is getting the objects momentum basically, then doing transform.right * velocity is multiplying the right momentum by whatever velocity is.
Essentially what I have is
void Start () {
GetComponent ().AddForce (transform.right * velocity);
}
This is useful for things like bullets, making it so as soon as they start existing they gain instant momentum, or for canons that need to shoot projectiles. Small numbers are kind of unoticable, so if you want any useful speeds you’ll need to do numbers at least above 100.
Good luck everyone!