Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Syzo · Nov 01, 2016 at 09:44 PM · unity 5prefabunity 2dshootingdirection

Prefab Direction - shooting

Hey! For some days I had a problem regarding one prefab direction, basically I'm building a 2D shooter game where the gun follows the mouse cursor.

My current problem is, when I'm facing right the bulletPrefab goes in the direction I want, but when I flip the character the bulletPrefab goes in the opposite direction... I tried to change it manually and it seems to be a problem with the rotation but I don't know how to solve it. Can you guys help? What I have:

     void Shoot() {
         // Obter posição do rato no "mundo"
         Vector2 mousePostion = new Vector2 (Camera.main.ScreenToWorldPoint(Input.mousePosition).x,Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
         Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
         RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePostion-firePointPosition, 100, whatToHit);
         if (Time.time >= timeToSpawnEffect)
         {
             Effect();
             timeToSpawnEffect = Time.time + 1/effectSpawnRate;
         }
 
         Debug.DrawLine (firePointPosition, (mousePostion-firePointPosition)*100);
         if (hit.collider != null) {
             Debug.DrawLine (firePointPosition, hit.point, Color.red);
             Debug.Log ("Hit:" + hit.collider.name);
         }
     }
 
     void Effect()
     {
         Transform playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
         Vector2 playerScale = playerTransform.localScale;
 
         if (playerScale.x > 0)
         {
             Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation);
         }
         else
         {
             Instantiate(BulletTrailPrefab, firePoint.position, Quaternion.Euler(firePoint.rotation.x, firePoint.rotation.y, -firePoint.rotation.z));
         } 
 
     }


// firePoint is a child object

Comment
Add comment · Show 5
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
avatar image spooneystone · Nov 01, 2016 at 10:08 PM 0
Share

What is this script attached to?

avatar image Syzo spooneystone · Nov 01, 2016 at 10:33 PM 0
Share

Attached to the arm that rotates

avatar image spooneystone · Nov 01, 2016 at 10:38 PM 0
Share

Try and not use the firePoint.positions and rotations, just use transform.position and the same for the rotations

avatar image sxnorthrop · Nov 01, 2016 at 10:40 PM 0
Share

I don't think you need to use Quaternion.Euler seeing as it's going to spawn the bullet at the fire point rotation anyways. Can you attach a picture of the problem?

avatar image Syzo sxnorthrop · Nov 01, 2016 at 10:55 PM 0
Share

Yes. This is what happens when I don't use Quaternion.Euler and only use the Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation); https://i.gyazo.com/0251d41d89c556da55270ee04f901b7e.gif

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sxnorthrop · Nov 02, 2016 at 12:13 AM

In the else statement for playerScale.x try Quaternion.LookRotation(-firePoint.forward) for the rotation.

Comment
Add comment · Show 2 · 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
avatar image Syzo · Nov 02, 2016 at 10:47 AM 0
Share

Hello again and thank you for answering! I tried it and the bullet always go forward now...

https://i.gyazo.com/2fe498770e651a76995a251b1d81ba6a.gif

avatar image sxnorthrop · Nov 02, 2016 at 06:43 PM 0
Share

You need to make sure that the firePoint is actually rotating with the gun, meaning the z axis of the firepoint should always be looking in the direction in front of the gun itself.

avatar image
0

Answer by Syzo · Nov 02, 2016 at 01:21 PM

Any solution out there?

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

121 People are following this question.

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

Related Questions

I want to spawn objects probabilistically. 0 Answers

UI inputfield not shows mobile keyboard on click 0 Answers

How do i fix my problem with minimizing/maximizing and just opening unity editor windows? 0 Answers

Slider.value doesn't show the value in real-time but after the execution of the script 1 Answer

OnTriggerEnter2D called without collision 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