How to rotate an instance relative to the parent and give it force?

Updated on June 30, 2018 in  [R] Scripts
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on June 29, 2018

I have a GameObject that rotates itself and has 4 guns that are children, I created the empty GameObject at the end of every gun and I would like to instantiate the bullet prefab the way the gun is rotated and give them force to go to that direction

Here is how the game looks: https://imgur.com/a/qpIEuRk

And this is my script to spawn, but don’t know how to give force:

public GameObject bullet;
 public Transform gun;
 public float time;
void Spawn()
 {
 Instantiate(bullet, transform.position, gun.transform.rotation);
 }
void Start()
 {
 Invoke("Spawn", time);
 }
  • Liked by
Reply
1 on June 30, 2018
It's something in the lines of:

void Spawn()

 {
 Instantiate(bullet, transform.position, gun.transform.rotation);
 Rigidbody rb = bullet.Getcomponent<Rigidbody>();
  rb.addForce(spawnObject.transform.forward * bulletforce);
 }
on June 30, 2018

This is my script for rotating the bullet after spawning and it works but there is no force or velocity, it just stays at the place where it’s spawned:

public Rigidbody2D bullet;
 public float time;
void Start()
 {
 Invoke("Spawn", time);
 }
void Spawn()
 {
 bullet = Instantiate(bullet);
 bullet.transform.rotation = transform.parent.rotation;
 }

 

 

rb.addForce(spawnObject.transform.forward * bulletforce);From Arilith

 This doesn’t work as autocomplete in Visual Studio doesn’t offer me spawnObject, it offers only Spawn and SpawnBullet (name of the class) and probably wouldn’t work as transform.forward is for 3D environment, my game is 2D

Show more replies
  • Liked by
Reply
Cancel