- Home /
How can I Instantiate a prefab (projectile) consistently from the character?
Basically, I have an enemy which periodically turns toward the player and fires a projectile or two. To be safe, I created an empty game object, childed to the enemy, and instance the projectiles from that.I've managed to get this working perfectly on a character I modeled. However, now I must rebuild it on a different model. Problem is, with this new model (which someone else made), half the time the projectile appears way below the floor. They still move in the right direction, but I can't figure out how to get the projectile to instance consistently from the character. I tried creating a cube and attaching that to the enemy,with the projectile script attached to that, so I could see if the object itself is moving, and the cube remains consistently at chest level to the enemy, even as the projectiles instantiate way below it.
var ogreProjectilePrefab : Rigidbody; var throwForce : int = 100;
function Release () { var projectile : Rigidbody = Instantiate (ogreProjectilePrefab, transform.position, transform.rotation); projectile.velocity = transform.TransformDirection(Vector3 (0, 0, throwForce)); //projectile.AddForce(projectile.transform.forward * throwForce);
using AddForce has the same result. What am I missing? I've also tried a different way:
var ogreProjectilePrefab : Rigidbody; var throwForce : int = 100; var target : Transform; function Release () { var player = GameObject.FindWithTag("Player");
target = player.transform;
direction = transform.position - target.transform.position ;
//direction.y= 0;
var Projectile : Rigidbody = Instantiate (ogreProjectilePrefab, transform.position, transform.rotation);
Projectile.velocity = transform.TransformDirection(direction);
//Physics.IgnoreCollision(Projectile.collider, transform.root.collider); //This keeps the collider on the projectile from messing with the collider on the player.
}
Still has the same problem, plus the projectiles shoot off sideways to the right, nowhere near the direction of the player. Please Help!
Ok, for you second piece of code, you are already calculating the direction in world space, meaning you don't have to call transform.TransformDirection
on your direction, ins$$anonymous$$d just set your velocity to direction :) Also please promise me you don't plan on calling FindWithTag
in any proper project :P Just the idea of forcing Unity to go through all objects in a scene just to find one, when it can be easily directly set in every situation, makes shivers run down my spine :)
Your answer
Follow this Question
Related Questions
How to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer
How can I get a GameObject's transform in game and set that to a variable? 1 Answer
Use GameObject's global position instead of local position 3 Answers
How do I get the opposite position of an object that spins around another object... 0 Answers
Objects Instantiating at wrong position 3 Answers