Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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:

https://gyazo.com/f3c06883be1c13df99e41a7d08e290cf

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer

Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers

Why is my instantiated prefab invisible ? 2 Answers

Instantied 2D Prefab Is Invisible 1 Answer

How to generate different diagonal platforms? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges