- Home /
 
 
               Question by 
               InfamousCupcake · Dec 12, 2016 at 07:50 AM · 
                c#2drotationinstantiateprefab  
              
 
              2D Projectile Not Firing Based on Rotation
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:
               Comment
              
 
               
              Answer by JxWolfe · Jan 02, 2019 at 10:37 PM
Well, probably a few things need to be fixed. //first script
      void Fire()
      {
          GameObject clone = Instantiate(toppings, shootSpot.position, shootSpot.rotation);
          // clone.transform.SetParent(shootSpot.transform);
       }
 
               This should now point your toppings in the dirrection shoot Spot is pointing - straight out of the cannon.
and this could be a problem //second script
      private void FixedUpdate()
      {
          rb.velocity = transform.forward * toppingSpeed * Time.deltaTime;
          
      }
 
               now after pointing it in the right direction, we should tell it to MOVE that way.
Hope that helps, if not, please inform me so I could relook over this code.
Your answer