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 Liakes · Mar 28, 2019 at 04:11 PM · 2d rotationprojectilesaddrelativeforce

Enemy's projectile is firing at the opposite direction when it's y-axis align with the player's

Hi, I am fairly new to unity and am currently facing a problem regarding my projectile's firing direction. I wanted the enemy to shoot a projectile aiming towards the player's current position and i achieved it by attaching a child gameobject with this script to the enemy public class HomingShots : MonoBehaviour {

 [SerializeField] GameObject enemyLazerPrefab;
 [SerializeField] float timeBetweenShots = 0.5f;
 [SerializeField] float projectileSpeed = 6f;
 bool firing = true;
 Player player;
 
 void Start ()
 {
     player = FindObjectOfType<Player>();
     StartCoroutine(FiringCoroutine());
 }

 private void FixedUpdate()
 {
     if (player)
     {
         if (!firing)
         {
             firing = true;
             StartCoroutine(FiringCoroutine());
         }
         transform.up = player.transform.position - transform.position;
     }
     else
     {
         firing = false;
         player = FindObjectOfType<Player>();
     }
 }

 IEnumerator FiringCoroutine()
 {
     while (firing)
     {
         yield return new WaitForSeconds(timeBetweenShots);
         Fire();
     }
 }

 private void Fire()
 {
     GameObject enemyLazer = Instantiate(enemyLazerPrefab, transform.position, transform.rotation);
     enemyLazer.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.up * projectileSpeed, ForceMode2D.Impulse);
 }

} It is working just fine if the y-axis of the player does not align the child's y-axis. However, if does align, the child's x rotation somehow becomes -180 and the projectile that is instantiated starts firing in the opposite direction as shown here alt text

Somehow despite the line of code that adds force in the direction of the projectile's positive y-axis, the projectile is moving with a velocity of +5(upwards) while its vector2.up is pointing downwards towards the player.

Please help me if you can.

Thanks for reading(This is my first post so if i happen to make any mistake please feel free to correct me.)

pic1.jpg (63.1 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

166 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 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

How do I add a relative force to the bottom of a cube? 1 Answer

how bullet pointing towards the target unity 1 Answer

Rotate camera and player with mouse 2d top-down shooter in C# 1 Answer

Character returns to idle animation when moving left or any different directions instead of facing the direction in where it moved 2 Answers

Changing 2D sprite rotation with min and max angles using mouse movement 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