I am trying to fire a bullet prefab from a player game object in the direction that the player is facing. I have been at this for two nights with no success. The bullet is a simple sphere with no rigidbody. I am trying to see if this can be done from the players script without the bullet having to have it’s own script to move. Here is the code I have to move my player and Instantiate the bullet…
public class MovePractice : MonoBehaviour
{
// Start is called before the first frame update
private float rotAngle;
private float moveSpeed;
private Renderer render;
public GameObject spherePrefab;
void Start()
{
render = GetComponent<Renderer>();
render.material.color = Color.grey;
rotAngle = 180;
moveSpeed = 5;
}
// Update is called once per frame
void Update()
{
MoveCube();
if (Input.GetKeyDown(KeyCode.Space))
{
var bullet = Instantiate(spherePrefab, transform.position, transform.rotation);
}
}
void MoveCube()
{
if (Input.GetKey("w"))
{
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
if (Input.GetKey("s"))
{
transform.Translate(new Vector3(0, 0, -moveSpeed * Time.deltaTime));
}
if (Input.GetKey("d"))
{
transform.Rotate(0, rotAngle * Time.deltaTime, 0);
}
if (Input.GetKey("a"))
{
transform.Rotate(0, -rotAngle * Time.deltaTime, 0);
}
}
}