- Home /
Projectile wont move
Here's the script, it's at the bottom.
var node : Transform;
var enemies : GameObject[];
var targetEnemy : GameObject;
var fireAt : Transform;
var spellTransform : Transform;
var minionSpeed : float;
var distance : float;
var attackTimer : float;
var minionHealth : int;
var minionDamage : int;
var distracted : boolean; // if it's do anything besides walking
function Start() {
minionSpeed = 1;
minionHealth = 300;
minionDamage = 25;
distracted = false;
targetEnemy = null;
attackTimer = 3f;
node = GameObject.FindGameObjectWithTag("Node").transform;
enemies = GameObject.FindGameObjectsWithTag("team2");
}
function Update() {
if(attackTimer > 0f) attackTimer -= Time.deltaTime;;
if(!distracted) {
transform.LookAt(node);
transform.position += (transform.forward * minionSpeed * Time.deltaTime);
}
if(enemyInRange()) {
distracted = true;
attackEnemy();
}
//Debug.Log("Closest Enemy: "+FindClosestEnemy().name+ " ---- Distance: " +distance);
}
var rotationSpeed2 = 7;
var moveSpeed2 = 10;
function enemyInRange() : boolean {
enemies = GameObject.FindGameObjectsWithTag("team2tower");
if(enemies.Length > 0) {
targetEnemy = enemies[0];
var dist = Vector3.Distance(transform.position, enemies[0].transform.position);
for(var i=0;i<enemies.Length;i++) {
var tempDist = Vector3.Distance(transform.position, enemies[i].transform.position);
if(tempDist < dist) {
targetEnemy = enemies[i];
//Debug.Log("Distance from enemy: "+tempDist);
}
}
}
if(tempDist < 5) {
return true;
}
return false;
}
function attackEnemy() {
if(attackTimer < .1f) {
sendAttack();
attackTimer = 3f;
}
}
function sendAttack() {
Debug.Log("Attack sent:");
fireAt = targetEnemy.transform;
var attack = Instantiate(spellTransform,this.transform.position,Quaternion.identity);
attack.rotation = Quaternion.Slerp(attack.rotation,
Quaternion.LookRotation(fireAt.position - attack.position), rotationSpeed2*Time.deltaTime);
attack.position += spellTransform.forward * moveSpeed2 * Time.deltaTime;
}
trying to figure out why it's not moving.
$$anonymous$$ore than sure. I've been editing it none stop and the changes apply aswell.
Also, that's standart to "$$anonymous$$ultiply" the vector 3 and a float, don't ask me. I took it straight from the wiki.
Ignore Ben's comment it is off base and adds nothing to your problem. That part of your code is at least syntactically correct. They don't seem to understand the C# language or Vector math.
Whoops, didn't realise you were adding a Vector3 not a float, sorry about... The reason your projectile won't move, is because nowhere in your script are you telling it to do so. sendAttack only instantiates one projectile and moves it once. You have to make another script to do the moving, or (a terrible idea that works) keep an array of spells, and update very one of them every frame.
Answer by vbbartlett · Feb 13, 2013 at 04:35 PM
Not sure where to begin!
The design seems incorrect. You have several transforms one being the spell transform, but it isn't pointing to anything. You don't locate another object to that position and rotation, you aren't instantiating an actual spell prefab... you are creating a transform... nothing else. On top of that once you create it you set its position and rotation once and it never updates. Transforms don't just update themselves.
You probably should start with creating a script that controls a spell, and apply it some some visual object then instantiate that. The script should then update itself each frame with where it wants to move, whether a target or a direction...
But, the script does exactly that...
attackEnemy() calls sendAttack() and attackEnemy() is called in Update(), therefor sendAttack() is ALSO ran through update. I am instantiating a transform (I have it set in the inspector, it spawn the item I wanted it to original spawn), I took this directly from another person's code that was released so I could make my own script, so i know that it works as I've seen it before. The way I'm writing everything I can't just have a script that controls a "Spell" because in reality it's just a basic auto attack that sends out a projectile. Although, I do have a script that is attatched to the spell once it is created and that handles all the collision.
Ok, that added info helps, but I still don't see where the new instanciated spellTransform gets updated every frame. It is created and set once but that transform doesn't update its position and rotation at any point in the code you have provided.
One point of note on your Slerp, if that code were to be called every frame, the rotation of your object would slow down and would never technically reach the lookat direction.
If that is what you want, fine, but normally I would have expected to save the Initial rotation and then increment the slerp float value by the delta time.
Another note since this appears to be moving if there is physics, you should code this movement in the FixedUpdate so that the physics moves correctly and the display doesn't jitter.