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 /
avatar image
0
Question by brok0de · Aug 08, 2018 at 12:21 AM · 2d2d-platformer2d-physicsbullets

Bullet Follows Player Orientation.

I have a bullet script that needs to travel forward relative to the player's orientation on the X axis, but the bullet prefab changes direction whenever the player does. Please help, because my brain hurts :L

  void FlipVelocity(bool isRight) {
         if (isRight)
         {
             sr.flipX = false;
             transform.Translate(velocity * Time.deltaTime, 0, 0);
         }
         else if (!isRight) {
             sr.flipX = true;
             transform.Translate(-velocity * Time.deltaTime, 0, 0);
         }
     }
 
     private void Update()
     {
         if (pc.isFacingRight)
         {
             FlipVelocity(true);
         }
         else if (!pc.isFacingRight){
             FlipVelocity(false);
         }
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by SpeakBeaver · Aug 08, 2018 at 01:00 AM

You can use Vector3.Lerp ( and set Z to 0 for 2D) to give a Direction to your Bullet.

And your bullet is changing of orientation maybe because she is the children of the player, have you check this ?

Hope it's help !

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
avatar image
0

Answer by brok0de · Aug 08, 2018 at 02:59 AM

Um I'm not sure if it's a child tbh. It is instantiated in the PlayerController. Would that make it a child? Also thanks for the advice! I'll see if it works :)

Comment
Add comment · Show 3 · 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 SpeakBeaver · Aug 08, 2018 at 03:59 PM 0
Share

to check if your bullet is not a child just Debug.Log the transform parent. It can be a child if your prefab is related to your player

avatar image brok0de SpeakBeaver · Aug 10, 2018 at 05:06 AM 0
Share

Nope :L The bullet has no parent. I figured as much tbh, my logic in the script isn't exactly explicit. But I have no clue how to write it. $$anonymous$$aybe I could check for a previous state on the player and work on serialization for the bullets? But that sounds really complicated for the kind of work I need the script to do. Oh well.

avatar image SpeakBeaver brok0de · Aug 10, 2018 at 03:40 PM 0
Share

If I had to do something similar, I think I would do a Coroutine with a given axis to follow or with a director vector if you have a trigger system. Something like that (I didn't try but juste to give you the idee)

 GameObject bulletPrefab;
 public void Update()
 {
     if (Input.Get$$anonymous$$ouseButtonDown(0)) //left clic
     {
         
         Vector3 playerPos = new Vector3(2, 2, 2); //Get position of your player
         GameObject newBullet = Instantiate(bulletPrefab);
         newBullet.transform.position = playerPos;
 
         float Xplayer = 1; //Get direction of your player x or -x 
         StartCoroutine($$anonymous$$yBulletCoroutine(newBullet, new Vector3(Xplayer, 0, 0)));
     }
 
 }
 
 private IEnumerator $$anonymous$$yBulletCoroutine(GameObject prefab, Vector3 direction)
 {
     //if you have no end juste make it infinit but not sure it's a good way to do ^^
     while (true) {
         float coefTranslate = 1; // to adjuste your speed
         prefab.transform.Translate(direction * coefTranslate);
     }
 
     //if you have a target position given in argument use insted Lerp and give a time too
 
     float time = 2;
     float actual = 0;
     Vector3 targetPos = new Vector3(1, 1, 1);
     while (actual < time)
     {
         prefab.transform.position = Vector3.Lerp(prefab.transform.position, targetPos, actual / time);
         actual += Time.deltaTime;
         yield return null;
     }
 }

With this technic you can have multiple bullet independant of the player If you don't know [Couroutine][2] it's really really usefull and I can only advice you to watch how it works because somehow you gonna need this one day.

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

89 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

Related Questions

Help Me! I want to make a pixel fairy! 1 Answer

Two Polygon2D Collider do not Collide with Each Other (Solved) 2 Answers

[2D] Sprite flickers/is choppy when moving 1 Answer

Farseer Physics and Character Control 0 Answers

Weird physics issue after disabling and re-enabling player gameobject. 3 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