• 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 EmilCarlssonArt · Jun 18 at 10:38 AM · spritesif-statementsspriterendererflipflips

Trying to make a sprite flip based on walking direction as well as camera position

Hey! :)

I'm trying to flip a sprite in a 3D environment. It should flip depending on the camera angle (the camera can rotate) and it should also flip depending on walking direction.

I have this script which have two variables (lastPosition to track when it moves, and fyRot is camera position) that I can't get to work at the same time. If I remove the one of them, the other one work. But if I both have them together, only the fyRot seem to flip the sprite.

Would really appreciate any help with this, I'm quite new to coding! Been trying to fix it for over a week. My best guess is that the camera flip overrides the lastPos flip. But haven't been able to figure out how to fix that.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class flippety_flippety : MonoBehaviour
 {
     [SerializeField] SpriteRenderer spriteRenderer;
     public static bool flipX;
 
 
     Vector3 lastPos;
     public Transform obj; // drag the object to monitor here
 
     public float threshold = 1.0f; // minimum displacement to recognize a 
 
 
     void Start()
     {
         SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
         lastPos = obj.position;
     }
 
 
 
 
     void Update()
     {
         float fYRot = Camera.main.transform.eulerAngles.y;
 
 
         Vector3 offset = obj.position - lastPos;
         if (offset.z > threshold)
         // code to execute when Z is increased
         {
             // update lastPos
             lastPos = obj.position;
             if (flipX == false)
             {
                 
                 spriteRenderer.flipX = true;
                 Debug.Log("TestingTrue");
             }
             else if (flipX == true)
             {
                
                 spriteRenderer.flipX = false;
                 Debug.Log("TestingFalse");
             }
 
         }
         // code to execute when Z is getting smaller
         else
         if (offset.z < -threshold)
         {
             // update lastPos
             lastPos = obj.position; 
 
             if (flipX == false)
             {
                 
                 spriteRenderer.flipX = true;
                 Debug.Log("TestingTrue2");
             } else if(flipX == true)
             {
                 
                 spriteRenderer.flipX = false;
                 Debug.Log("TestingFalse2");
             }
 
         }
 
 
 
 
 
         if (fYRot >= 180)
         {
             if (flipX == false)
             {
 
                 spriteRenderer.flipX = true;
                 Debug.Log("TestingTrue3");
             }
             else if (flipX == true)
             {
 
                 spriteRenderer.flipX = false;
                 Debug.Log("TestingFalse3");
             }
         }
         else if (fYRot >= -180)
         {
             if (flipX == true)
             {
 
                 spriteRenderer.flipX = true;
                 Debug.Log("TestingTrue4");
             }
             else if (flipX == false)
             {
 
                 spriteRenderer.flipX = false;
                 Debug.Log("TestingFalse4");
             }
         }
     }
 }

Comment
Add comment · 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 tormentoarmagedoom · Jun 23 at 09:19 AM 2
Share

too much code, too much empty lines... if want people to read your code, simplify if. Explain what r you doing, waste your time editing the post so people dont have to waste their.

Make some paint explaining what you want, what you havem what is happening...

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.

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

142 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

Related Questions

Could someone please help with making an enemy sprite flip when going the other way? 1 Answer

Warp Image/Sprite Anchors? 1 Answer

how can i access multiple sprites using a single variable? 1 Answer

What in this script is causing the sprites to face backwards rather than forwards? 1 Answer

Why is my sprite not the original resolution? 2 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