Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

Trying to move an instantiated object with a Rigidbody2D and AddForce

$
0
0
I am setting up a gameobject that uses a coroutine to endlessly spawn bullets, and I want to make it so that when I rotate the gameobject it also changes the direction the bullets are spawning and moving at. Currently, the projectiles do not move at all. The first if statement is determined by a public bool laser, which I have toggled in the editor for specific prefabs. I have declared _bullet and _laser as GameObjects and serialized them so I can plug them in the editor. I have also declared a public RigidBody rb but haven't assigned anything to it in the editor. The Rigidbodies are kinematic. IEnumerator Shoot() { while (true) { yield return new WaitForSeconds(1); if (laser == false) { Instantiate(_bullet, transform.position + new Vector3(0, 0, 0), Quaternion.identity); rb = _bullet.GetComponent(); rb.AddForce(new Vector2(10, 0)); } else if (laser == true) { Instantiate(_laser, transform.position + new Vector3(0, 0, 0), Quaternion.identity); rb = _laser.GetComponent(); rb.AddForce(new Vector2(10, 0)); } } }

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>