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
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.