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 JayJonesJamie · Aug 18, 2019 at 09:57 PM · shootingparentchildbulletflip

How to flip my Bullet with my Sprite

So I have my parent (which is the sprite) and I have my child (firepoint of the bullet) but I don't know how to synch them to flip together, when my character flips the fire position stays in the same pos.

Heres the shoot script attached to the fire point (which is the child of the sprite)

 // Shoot needs to know the direction, so we just ask the renderer if we are flipped
 // Link the player sprite renderer to this in inspector
 [SerializeField] public SpriteRenderer mySpriteRenderer;


 private void Update()
 {
     if (Input.GetKeyDown(KeyCode.K))
     {
         GameObject go = Instantiate(projectilePrefab, transform.position, transform.rotation);

         Rigidbody2D rb2d = go.GetComponent<Rigidbody2D>();

         // Lets get our direction from renderer
         int facing = mySpriteRenderer.flipX == true ? -1 : 1;

         rb2d.velocity = transform.TransformDirection(new Vector3(20f, 0, 5f)) * facing;
     }
 }

}

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
Best Answer

Answer by I_Am_Err00r · Aug 20, 2019 at 02:02 PM

You need to better define what the player direction is on the player; I'm guessing you don't have a script on your player that manages the different states; the easiest way to define direction is through the player movement, and this script should be included into whatever script you have for player movement.


I have a solution, it's based on variables you defined in this script though and might need you to tweak the variable names:

   public bool facingLeft;

   private void Start()
   {
         if(facingLeft == true)
         {
               facingLeft = false;
               Flip();
         }
   }
   
   private void FixedUpdate()
   {
        CheckDirection();
   }

   public void CheckDirection()
     {
         if(facingLeft != true && rb2d.velocity.x < 0)
            {
                 Flip();
            }
          else if(facingLeft == true && rb2d.velocity.x > 0)
          {
                Flip();
          }
     }
 
     private void Flip()
     {
          facingLeft = !facingLeft;
          Vector2 theScale = new Vector2();
          theScale = gameObject.transform.localScale;
          theScale.x *= -1;
          gameObject.transform.localScale = theScale
     }

If you set that up correctly, all children objects will conform to the parent and flip appropriately; I've also set this up so that if you want to have the character facing left at the start, you just check that public bool I set and that will ensure that when you load your player, it will face the direction you have chosen through that bool.

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 JayJonesJamie · Aug 20, 2019 at 06:56 PM 0
Share

Thanks for the help

avatar image JayJonesJamie · Aug 29, 2019 at 12:06 PM 0
Share

Since I added this to my script I'm stuck in my jump animation and can no longer jump. Please help =)

avatar image I_Am_Err00r JayJonesJamie · Aug 29, 2019 at 03:44 PM 0
Share

If you are having trouble with your jump I can say with confidence that it is unrelated to this solution and purely coincidental if you noticed the problem when you added this; this script purely faces the sprite in the direction in which they are moving based on velocity value from the rigidbody2D, it only reads values from the rigidbody2d componenet and it does not change any of the values that would somehow affect your jump.


If you want help, please post a new question with what is going on, provide the script that contains the jump command, and I'll take a look at it from there.

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

112 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

Related Questions

Make a simple tree 1 Answer

Child object not flipping with parent? 1 Answer

Making an object follow another object (bullets following a player) 1 Answer

Object scale/rotation changes when parented to flipped object 0 Answers

accessing all of a colliders parents children 1 Answer


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