- Home /
Projectile not 'projecting' because i have to use "var xxx : GameObject = Instantiate"
I am using the following code:
var blaster : GameObject = Instantiate(blasterPrefab, GameObject.Find("blasterPoint").transform.position, Quaternion.identity);
// Propells blasted ammunition forward
blaster.rigidbody.AddForce(transform.forward * 2000);
//end fire weapon phase
but the projectile (blaster) does not blast forward since I added "GameObject" as the variable type (needed to do this to allow program to run on iPhone). How do I add the forward momentum to the blasted object when I use the code as I do above?
Answer by Ecnalyr · Mar 17, 2011 at 04:40 AM
I found a work-around:
function Update () {
transform.LookAt(Clicktomove.blackhole);//blackhole is my target (aka forward direction)
rigidbody.AddForce(transform.forward * 200);
}
I added the above script to the actual prefab that acts as the projectile itself.
Although that will work, its unneeded. As I said, try using
blaster.rigidbody.AddForce(blaster.transform.forward * 2000);
Unless you want to keep a constant force for some time, the method I just showed you is cheaper. Every GameObject has a transform.
Using that still gives the error "Cannot cast from source type to destination type." I would love a cheaper alternative that works the way you are attempting to go.
Answer by Lab013 · Mar 17, 2011 at 03:37 AM
Use blaster.transform.forward.
var blaster : GameObject = Instantiate(blasterPrefab, GameObject.Find("blasterPoint").transform.position, Quaternion.identity);
// Propells blasted ammunition forward blaster.transform.forward * 2000;
//end fire weapon phase
This returns the error "Cannot cast from source type to destination type."
?
Your answer
Follow this Question
Related Questions
AddForce to an object with static velocity 0 Answers
`Inventory' does not contain a definition for `Add' 2 Answers
KnockBack Effect. 1 Answer
Add force to GameObject 2 Answers
Is it Possible to Bring back a Destroyed GameObject? 3 Answers