- Home /
Scripting question about rotating on collision
Basically I am making a shooting level that when you hit the target a flag attached to the target will rotate up to show that it has been hit. Basically what I am trying to do now is make code that I can attach to the target, and when the projectile hit's it, the the specified flag will rotate until the axis is about the same as an empty reference object I am using. Here is the code I have so far but does not work and I am not sure why. Any help you can give would be great! Also I attached the code I am using for the launcher and projectiles in case that helps.
Rotation code I want to be able to attach to target
// Target to get angle from var myOb : Transform; // Empty reference object to rotate towards var refOb : Transform; // Flag to rotate var rotOb : Transform;
function OnCollisionEnter(collision : Collision){
// axis of empty ref object to rotate towards var targetDir = refOb.up; //axis of flag I want to rotate var my : Transform = collision.transform;
//get angle between the two
var angle : float = Vector3.Angle( targetDir, my.up );
Debug.Log(angle);
// call coroutine slow rotate
SlowRotate(my,targetDir);
}
function SlowRotate( myOb : Transform, targetDir : Vector3 ) { // get the angle between those two objects var angle : float = Vector3.Angle( targetDir, myOb.up ); while ( angle > 0.5 ) { // Rotate some rotOb.Rotate(Vector3.right*30*Time.deltaTime); // Update angle angle = Vector3.Angle( targetDir, myOb.up ); // Wait 1 frame yield; }
} Launcher code:
var projectile : Rigidbody; var speed = 20;
function Update () { // Put this in your update function if (Input.GetButtonDown("Fire1")) {
// Instantiate the projectile at the position and rotation of this transform var clone : Rigidbody; clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current // object's Z axis clone.velocity = transform.TransformDirection (Vector3.forward * speed); }
} Code for projectile:
// 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)
Your answer
Follow this Question
Related Questions
How to make two objects mimic each other? 1 Answer
move till collision 1 Answer
Store initial rotation in JavaScript 2 Answers
Ignore parent Rotation 1 Answer