- Home /
cloning bullet object
Hi. Im trying to clone a gameobject that has a rigidbody everytime I press the mouse button for a bullet fire effect. I just dont really know how to tackle this problem, because evertime it clones, I want my graphic to show up on screen and I dont think i can achieve this with just cloning a rigidbody - any help? I dont need the exact code, just an idea of how this is usually accomplished. Thanks! -Alec
BY THE WAY the game is in 2D
public function fire(){
var clone : Rigidbody2D;
clone = Instantiate(bullet, playerPos, transform.rotation);
clone.velocity = transform.TransformDirection(Vector2.up * pistol.speed);
}
Answer by Nodgez · Feb 18, 2014 at 10:35 PM
You can Instantiate Game objects as well as rigidbodies.
public function fire(){
var clone : GameObject;
clone = Instantiate(bullet, playerPos, transform.rotation);
clone.rigidbody.velocity = transform.TransformDirection(Vector2.up * pistol.speed);
}
this will work as long as you're bullet is a GameObject
And assu$$anonymous$$g 'clone' is initialized somewhere. Usuall 'clone' would be declared outside the function at the top of the file and initialized by drag and drop in the inspector.
Note the original code would work also. A Rigidbody cannot exist without a game object, so Instantiate is creating the game object as well.
Your answer
Follow this Question
Related Questions
How do i Clone a gameObject when pressing "F"? 2 Answers
Destroy Child of Game Object 2 Answers