- Home /
Rotation problem with spawning bullet
I have had this problem a couple of times and i always forget how i fix it.What i am trying to do is make a bullet spawn in front of the barrel and add a force to move it forward and no matter what i do the bullet just moves to the left of the gun... Can someone please explain to me(EXPLAIN TO ME) how to fix this so i will always have a reference to solve this problem.
oh yea the code...
var bullettype : Transform;
var spawnspot : Transform;
var bulletspeed : float = 500.0;
function Update () {
if(Input.GetButtonUp("Fire1")){
var shot = Instantiate(bullettype,spawnspot.transform.position,spawnspot.transform.rotation);
shot.rigidbody.AddForce(transform.forward * bulletspeed);
Debug.Log("Fired");
}
Please format your code appropriately when posting it (the 101010 button in the formatting box) as it makes it easier for people to read.
ok thanks i didnt really realize that so sorry ill get it next time :D
Answer by Chris D · Apr 22, 2011 at 07:39 PM
What it sounds like is that, while you're spawning the bullet in at the spawnspot's position and rotation, spawnspot's transform might not be pointing in the correct direction in the first place; I'd check that first.
If that's not the case, try pausing the game directly after the bullet is spawned and seeing how it's axes differ from the spawnspot's transform - that should give you a better indication of what's off, relation-wise.
Answer by loopyllama · Apr 22, 2011 at 08:33 PM
your bullet rotation is set to the rotation of spawnspot but you are adding force in the direction of the object this script is attached to. try spawnspot.transform.forward instead of transform.forward
and I don't want to say you don't need a rigidbody with physics for your bullets...but you probably don't need a rigidbody with physics for your bullets. just update the bullet transform each frame rather than using the physics to drive the bullet.
Your answer
Follow this Question
Related Questions
Airplane goes weird on collision (Rigidbody) 1 Answer
Rotating Camera, and Movement help. 1 Answer
Instantiating bullet and moving forward with rigidbody 0 Answers
Prevent Rigidbody From Rotating 3 Answers
I need help with rotation 2 Answers