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.