Ok so I have this object that is the player, it has four tubes on the sides(See Pictures), and I want to create bullets that go through the tubes and out, but my player object is also rotating so the tubes are constantly moving around.
Questions:
So I am trying to figure out how to go about creating a bullet that will rotate with the player until it fires out of the tube, then it goes in a straight line.
Or
Create a bullet at the end of the tube right as it lines up with the grid, and have it fire out in a straight line…(I’m Leaning Toward This Option, But I don’t know how to go about the coding for it…)
Please If you can help, Please Do So!!
Thanks
Images:
You are going to want to create an empty GameObject for each place that should shoot a bullet. You also need to make sure you have the bullet saved in your asset folder so that it can be used. Here is a very simple script to be put on the shoot points.
public GameObject bulletPrefab; //your bullet must have a Rigidbody attached to it
public float bulletVelocity; //this is how fast your bullet will travel
void Update () {
// checks if the left mouse button has been pressed
if (Input.GetButtonDown("Fire1"))
{
//intantiates the bullet at the shootPoint's position and rotation
Instantiate(bulletPrefab, transform.position, transform.rotation);
//grabs the Rigidbody from the bullet (the allow for physics to be applied to the GameObject)
Rigidbody rb = bulletPrefab.GetComponent<Rigidbody>();
//applies a forward force to the rigidbody, shootig it forward
rb.AddForce(transform.forward * bulletVelocity * Time.deltaTime);
}
}
Make sure not to just copy/paste, but look up some of the terms used within the code to help you learn.