Instantiate GameObject with Velocity
I am trying to instantiate a bullet from a script and then on that same script change the gameObject's velocity.
I know there are some answers on this but a lot of it is in JavaScript and the other stuff doesn't seem to be working for me.
clone = Instantiate (bullet, new Vector3(gunTip.position.x,gunTip.position.y,transform.position.z), Quaternion.identity);
bullet.GetComponent<bullet> ().damage = GetComponentInChildren<newGun> ().damAge;
if (GetComponent<testMove> ().facingRight == true) {
clone.velocity = new Vector2(35, 0);
} else {
clone.velocity = new Vector2(-35,0);
}
I don't know what clone should be defined as in order to also change its velocity
when you instantiate your bullet, ins$$anonymous$$d of using Quaternion.Identity, use the guns current rotation. Then set the velocity of the bullet.
clone.velocity = 35 * Vector3.Forward;
@$$anonymous$$agius96 Sorry I don't think I understand. I changed the velocity like you said, but I was getting an error. I still have clone = Instantiate (bullet, new Vector3(gunTip.position.x,gunTip.position.y,transform.position.z), Quaternion.identity); written but is "clone" a filler word which I should change? And how should I create the object/what ever it is considered (what ever clone is) after being equal to the instantiate.
clone.velocity = 35 * Vector3.Forward;
This will always equal (0,0,35). I think you meant:
clone.velocity = 35 * clone.transform.forward;
matt- regarding the instantiate, I think this is what he was talking about.
clone = Instantiate (bullet, new Vector3(gunTip.position.x,gunTip.position.y,transform.position.z), gunTip.rotation);
or perhaps...
clone = Instantiate (bullet, new Vector3(gunTip.position.x,gunTip.position.y,transform.position.z), transform.rotation);
Answer by matthewse19 · Feb 25, 2016 at 09:57 PM
Thanks for the help! Here's what I ended up writing if anyone who also needs help is wondering:
GameObject clone;
void shoot(){
timer = 0;
GetComponent<testMove>().ammo -=1;
gunTip = transform.GetChild (2).FindChild ("GunTip").transform;
clone = (GameObject) Instantiate (bullet, new Vector3(gunTip.position.x,gunTip.position.y,transform.position.z), Quaternion.Euler (0,0,0));
clone.GetComponent<Rigidbody2D> ().velocity = 35 * transform.localScale.x * clone.transform.right;
bullet.GetComponent<bullet> ().damage = GetComponentInChildren<newGun> ().damAge;
}
I think there may have been some variables like timer and gunTip which I didn't define in this code but that wasn't the problem I had with. What I had to do was define clone as a GameObject and put (GameObject) in front of Instantiate to make it work. I also didn't instantiate the bullet based on the player's rotation because I am working in 2D (something that I probably should have mentioned). So instead for the bullets velocity I also multiplied it by the player's x scale because that is the axis I use to flip the player's sprite when they change direction.
Also I realized that there is some poor coding I did which I can shorten up but I was leaving it longer so I could adjust some individual values for testing.
Your answer
Follow this Question
Related Questions
How does one continuously move an object forward without using velocity? 1 Answer
Instantiate position for shooting projectile is way off. Could I get some help? 0 Answers
Spawning objects with a fixed velocity in 3D? 0 Answers
RigidBody2D velocity randomly drops to zero 1 Answer
rigidbody.velocity.x doesn't work :( 2 Answers