Hi, i´ve got a canon that i can rotate around its Z Axis. That Works fine.
In the canons center i instantiate a bullet (just a ball). Now how to get the bullet to exactly the same direction like the canon.
my Instantiation, where “this” is the canon and “go” is the bullet
go.transform.position = new Vector3 (this.gameObject.transform.position.x,
this.gameObject.transform.position.y,
this.gameObject.transform.position.z );
is identical to this:
go.transform.position = transform.position;
Now on to rotation. I am assuming this is a 2D game which is why the rotation occurs on the Z axis.
You can use the cannon’s transform.up to get your desired shooting direction. The script should look something like this:
If it still does not shoot in the desired direction, the problem might have to do with the way you are rotating the cannon, or the actual art of the cannon being lopsided, among a whole other host of potential causes
transform.up is basically a conversion of the object’s green axis facing direction from Local to Global. Not that its changing the facing direction, but is calculating what the facing direction would be in Global. You can do the same with all the other pre-defined directions such as Down, Forward, Back, Left, and Right, if you need stuff to go in those other directions.
AddRelativeForce should have done what you intended really. Maybe defining the direction yourself and applying it to AddForce is more precise.
Even if I give you a brief answer, you probably still will not understand. The entirety of the topic is “Euclidean Vector” in math. I highly recommend you to read up a little on it. even a Wikipedia search will be greatly helpful for you in the long run.