- Home /
How to create homing missile that follows path using Unity and iTween
Greetings everyone!
Been messing around with Unity and iTween and bought some of the examples and have a character moving along a path and jumping all fine and dandy.
Problem arose when I decided to have the character shoot. Originally I used a modified version of the code from FPS tutorial, but that just fires in a direction. What I want is for the projectile to be created and then follow down the same path the player is on but I'm having problems getting the syntax right for the iTween.PutOnPath. Specifically, with getting the players current position.
Here's the current code and any help would be greatly appreciated.
var projectile : GameObject; var initialSpeed = 20.0; var reloadTime = 0.5; var ammoCount = 20; private var lastShot = -10.0;
//Path Attach var path : Transform[]; private var pathPosition : float=0 ; public var character : Transform; private var value : float; public var rocketSpeed : float; private var closest : GameObject; private var coordinateOnPath : Vector3; var pathPercent : float;
function Update(){ FindClosestEnemy(); DetectKeys(); }
// Find the name of the closest enemy function FindClosestEnemy () : GameObject {
// Find all game objects with tag Enemy var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("Enemy"); var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var go : GameObject in gos) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest; }
function DetectKeys(){ //fire if (Input.GetKeyDown("left ctrl") && Time.time > reloadTime + lastShot && ammoCount > 0) { Debug.Log ("FIRE"); // create a new projectile, use the same position and rotation as the Launcher. var instantiatedProjectile : GameObject = Instantiate (projectile,transform.position, transform.rotation);
//Find Character Location on Path
pathPercent = pathPosition%1;
coordinateOnPath = (iTween.PointOnPath(path,pathPercent));
//Place on Path
iTween.PutOnPath(projectile,path, coordinateOnPath);
//Orient object to path
transform.LookAt(iTween.PointOnPath(path,value+.05));
//Make it a RigidBody
instantiatedProjectile.AddComponent ("Rigidbody");
instantiatedProjectile.AddComponent ("CapsuleCollider");
// Move Rocket to Closest Enemy
iTween.MoveTo(closest, transform.position, rocketSpeed);
//Original Movement Code
//instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
//Ignore Collisions between object and Projectile
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
//Cooldown and Reduce Ammo Count
lastShot = Time.time;
ammoCount--;
}
}
//Just making your code better looking, sorry for the edit.
Thanks for the cleanup. Any idea on how to fix it so that it not only looks pretty but works?
Answer by luozitian · Jan 10, 2012 at 10:32 AM
As my undertanding, function MoveTo of itween works only on start instead of update,so, that is the problem. my suggestion is to have on script to create prefab that move by itween and you need create path on runtime then it may get the effect you need
Your answer
Follow this Question
Related Questions
Go back to first node (itween + moveto) 2 Answers
itween MoveTo IndexOutOfRangeException 0 Answers
iTween Camera Jitter while moving on path 2 Answers
How to move an object on a random path with iTween 1 Answer
Help With Homing Missiles - Annoying! 3 Answers