• 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
Question by animationisart · Mar 30, 2016 at 08:43 PM · 2d game2d sprites2d collision

OnCollision2D Sprite Flip C#

I want to create a non-player controller sprite flip when they get from on collider to another so I can have a patrolling enemy, but I can't get the sprite to flip. Any suggestions?

using UnityEngine; using System.Collections;

public class JousterController : MonoBehaviour {

 public Vector3 pointB;
 Animator anim;
 public bool facingRight;
 
 // Use this for initialization
 IEnumerator Start () {
         var pointA = transform.position;
         while(true)
         {
             yield return StartCoroutine(MoveObject(transform, pointA, pointB, 3.0f));
             yield return StartCoroutine(MoveObject(transform, pointB, pointA, 3.0f));
         }    
 }
 
 IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
     {
         var i= 0.0f;
         var rate= 1.0f/time;
         while(i < 1.0f)
             {
                 i += Time.deltaTime * rate;
                 thisTransform.position = Vector3.Lerp(startPos, endPos, i);
                 yield return null;
             }
     }

 void CollisionEnter2D(Collision2D col)
 {
     if (col.gameObject.tag == "left")
     {
         facingRight = true;
         Flip();
     }
     
     if (col.gameObject.tag == "right")
     {
         facingRight = false;
         Flip();
     }
 }
 
     void Flip(){
      facingRight = !facingRight;
      Vector3 theScale=transform.localScale;
      theScale.x *= -1;
      transform.localScale = theScale;
      
  }

}

Comment

People who like this

0 Show 1
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 Abhiroop-Tandon · Mar 31, 2016 at 09:10 AM 0
Share

Are you sure sure about the tags that you are checking (left and right) ?? Also i dont get the use of facingRight in your Flip method. Just try to debug and see if it is even going in the CollisionEnter2D or not or if it is even reaching inside the Flip method. I dont see what you are doing wrong, i will only say you need bit more debugging on what you have.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by IAMBATMAN · Mar 31, 2016 at 07:49 AM

Can't you use

transform.eulerAngles = new Vector3(0,180,0);

and transform.eulerAngles = new Vector3(0,0,0);

to reset it back to normal?

Also you could make a new variable of type SpriteRenderer called spriteRenderer and In Start set it to the spriteRenderer on your object with

spriteRenderer = GetComponent (SpriteRenderer);

and then you could use spriteRenderer.flipY = true; but this wouldn't flip your collider.

Hope this helps :)

Comment

People who like this

0 Show 1 · 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 animationisart · Mar 31, 2016 at 06:28 PM 0
Share

I tried that and it didn't work, but I might try the sprite renderer to see it that works. I don't think I need the collider to be flipped

avatar image

Answer by dapa5678 · Mar 30, 2016 at 10:34 PM

You need to use eulerAngles so something like this.

 facingRight = !facingRight;
       
         transform.eulerAngles = new Vector2(int,int);

  




Comment

People who like this

0 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 animationisart · Mar 31, 2016 at 07:10 PM 0
Share

alt text

I get this error when I tried that. Is there something I'm supposed to be putting in the ()? Sorry, I'm really sick so I'm probably just being stupid.

screen-shot-2016-03-31-at-24428-pm.png (13.3 kB)
avatar image dapa5678 animationisart · Mar 31, 2016 at 10:13 PM 0
Share

This works fine

 GetComponent<SpriteRenderer>().flipX = true;

hope this helps

David :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Sprite colors not displaying properly after I click play 2 Answers

2D Sprite drifting down, otherwise passes through walls - how do I fix these issues? 0 Answers

Unity 2d - moving objects with the mouse 0 Answers

[2D] How to detect if player is grounded on slope? 1 Answer

Why my ball acts if there is a hidden collider? 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