I’m currently making a 2D arcade game with a cannon that rotates within specific values to fire toward incoming objects. I have been unable to sync the direction of the instaniated prefab with the cannon. What am I doing wrong?
Player Script
public class PlayerController : MonoBehaviour {
public float rotateSpeed = 30f;
public float leftRotationClamp;
public float rightRotationClamp;
public GameObject toppings;
public Transform shootSpot;
private Toppings toppingsScript;
private float rotationZ = 0f;
private void Start()
{
toppingsScript = FindObjectOfType<Toppings>();
}
private void Update()
{
}
private void FixedUpdate()
{
LockedRotation();
if (Input.GetKeyDown(KeyCode.Space))
{
Fire();
}
}
private void LockedRotation()
{
rotationZ += Input.GetAxis("Horizontal") * rotateSpeed;
rotationZ = Mathf.Clamp(rotationZ, leftRotationClamp, rightRotationClamp);
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, -rotationZ);
}
void Fire()
{
GameObject clone = Instantiate(toppings, shootSpot.position, Quaternion.identity);
// clone.transform.SetParent(shootSpot.transform);
}
Instantiated Prefab Script
public class Toppings : MonoBehaviour {
public float toppingSpeed = 5f;
public GameObject shootSpot;
private Rigidbody2D rb;
private PlayerController player;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
player = GameObject.FindObjectOfType<PlayerController>();
if (shootSpot == null)
{
shootSpot = GameObject.FindGameObjectWithTag("shootSpot");
}
}
private void FixedUpdate()
{
rb.velocity = transform.up * toppingSpeed * Time.deltaTime;
}
This is what is currently happening: