Quantcast
Channel: Questions in topic: "gun"
Viewing all articles
Browse latest Browse all 592

Objects instantiate at prefab location instead of playtime location

$
0
0
When I instantiate my bullets from my gun at my gun barrel in the Shoot() function it instantiates at the (0,0,0) location it is on my prefab object. But when I instantiate it in the Update function it instantiates the bullets at the runtime barrel actual location. If I print the barrel position in the Shoot function it says (0,0,0) but in the update function, it does the live position. using UnityEngine; public class Weapon : MonoBehaviour { public int damage; public int range; public int speed; public Transform barrel; //shotgun public float spread; public int bulletCount; public Camera fpsCam; public GameObject bullet; private void Start() { barrel = gameObject.GetComponentInChildren(); } public void Shoot() { Transform bt = barrel.transform; for (int i = 0; i < bulletCount; i++) { print("L" + barrel.transform.position); var randomNumberX = Random.Range(-spread, spread); var randomNumberY = Random.Range(-spread, spread); GameObject b = Instantiate(bullet, transform.position, transform.rotation); b.transform.Rotate(randomNumberX, randomNumberY, 0); Rigidbody rb = b.GetComponent(); rb.AddForce(transform.forward * speed * Time.deltaTime); } } private void Update() { } }

Viewing all articles
Browse latest Browse all 592

Trending Articles



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