• 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

  • Sort: 
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 :)

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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Shooter 2D - Problem with rotation of bullet to face direction (velocity) 2 Answers

How to make a stickman not dismember itself 0 Answers

How do I make rule tiles using 2D Tilemap Editor 0 Answers

RayCast or LineCast can't detect Enemy!! Unity 2D 0 Answers

Help me with the 2d lights! 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