- Home /
my rocket fires in the wrong direction
here's my rocket launcher code:
var projectile : Rigidbody; var initialSpeed = 20.0; var reloadTime = 0.5; var ammoCount = 20; private var lastShot = -10.0;
function Fire () { // Did the time exceed the reload time? if (Time.time > reloadTime + lastShot && ammoCount > 0) { // create a new projectile, use the same position and rotation as the Launcher. var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
ammoCount--;
}
}
and here's my rocket code:
// The reference to the explosion prefab var explosion : GameObject; var timeOut = 3.0;
// Kill the rocket after a while automatically function Start () { Invoke("Kill", timeOut); }
function OnCollisionEnter (collision : Collision) { // Instantiate explosion at the impact point and rotate the explosion // so that the y-axis faces along the surface normal var contact : ContactPoint = collision.contacts[0]; var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal); Instantiate (explosion, contact.point, rotation);
// And kill our selves
Kill ();
}
function Kill () { // Stop emitting particles in any children var emitter : ParticleEmitter= GetComponentInChildren(ParticleEmitter); if (emitter) emitter.emit = false;
// Detach children - We do this to detach the trail rendererer which should be set up to auto destruct
transform.DetachChildren();
// Destroy the projectile
Destroy(gameObject);
}
@script RequireComponent (Rigidbody)
so basically what happens is that the rocket fires in the wrong direction and more towards me
can you please format ALL your code, it's very hard to read.
Answer by Guru.du · Jul 01, 2010 at 06:15 PM
It appears that the orientation of whatever the script is attached to isn't right...for example, if u have a spawn point at the end of the rocket launcher it is probably rotated the wrong way. Instead of pointing away from you, it is pointing more toward you. Or a situation similar to that.
Answer by Mike 3 · Jul 01, 2010 at 05:51 PM
The code seems to be right (Though personally i'd use = transform.forward * initialSpeed; for the velocity just because it's a little easier to read - I aways get Transform Direction and InverseTransformDirection mixed up).
The only thing I can think of is that the forward vector of the missile launcher isn't pointing in the right direction
Answer by creative72 · Jul 01, 2010 at 05:56 PM
there are many things that may be creating this problem, the one that seems most likely is you may have your player backwards in local vs global space or something like that. making your initialSpeed var = -20 should do the trick.
Answer by creative72 · Jul 01, 2010 at 08:07 PM
there are many things that may be creating this problem, the one that seems most likely is you may have your player backwards in local vs global space or something like that. making your initialSpeed var = -20 should do the trick.
umm, I only started this project a couple of days ago - and im learning off tuts so could you please just show me where the speed is so i can change it - that'd be great thanks
i found the speed and set it to -20 but now my rocket fires in the right direction but flies backwards - ill keep notifying u but would simply rotating it do the trick?
Your answer
Follow this Question
Related Questions
adding damage to my rocket 3 Answers
Fire bullet into the direction my player is facing 2D 2 Answers
Raycast in the wrong way 3 Answers
My bullets arent going the right direction 1 Answer
2d fire from player towards mouse 1 Answer