Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Peteman · Jan 07, 2015 at 04:56 AM · spritesprite-animation

Distracting Sprite flip

I'm making a small, proof of concept game for myself but I am running in to an issue. The character can aim her gun depending on where the mouse is, so there are different sprites whether she is moving forward or backwards. I'll admit, I'm in the early stages, but right now, when I turn her around, there is a brief moment where the character faces the new direction before reorienting herself to face the mouse. I find this very distracting and would like to know if there is anything I can do to negate this, like delay the update so it waits until the next frame to flip the sprite.

Thanks for your help.

Comment
Add comment · Show 2
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 kevinspawner · Jan 07, 2015 at 07:42 AM 0
Share

Hi there. Information you provided is not detail. Are you using animation Controller?

avatar image Pixzle · Jan 07, 2015 at 07:49 AM 0
Share

Do you rotate the character by script? If so, we probably need the code.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Juice-Tin · Jan 07, 2015 at 08:45 AM

If you're using transform.LookAt(), that's almost always a bad choice as your object will just blindly stare at the other object.

Instead you should manually look up how to properly rotate objects using their rotation property, so you have control over which axis' are being used.

(IE. They will rotate in X or Y axis, but not Z)

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 Peteman · Jan 07, 2015 at 04:28 PM

I'm using an animator component.

Here's what the code looks like:

 using UnityEngine;
 using System.Collections;
 
 public class CharacterControllerScript : MonoBehaviour {
 
     public float jumpForce = 7f;
     public float maxSpeed = 8f;
     public bool facingRight;
     // Use this for initialization
     Animator anim;
 
     public bool grounded = false;
     public bool doubleJumped = false;
     public Transform groundCheck;
     float groundRadius = 0.2f;
 
 
     public LayerMask whatIsGround;
     //checks what can the character land on.
 
     Transform armGraphics;
     Transform playerGraphics;
 
     void Start () {
         anim = GetComponent<Animator>();
 
         playerGraphics = transform.FindChild("Graphics");
         
     
     }
 
     //test
     // Update is called once per frame
     void FixedUpdate () 
     {
 
 
         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
         //checks if there is something underneath
         if (grounded)
         {
             doubleJumped = false;
         }
 
         anim.SetBool ("Ground", grounded);
         //tells the Ground variable in the animation controller that speed is this.
 
         float move = Input.GetAxis ("Horizontal");
         //gets the movement direction
 
 
 
 
         anim.SetFloat ("Speed", Mathf.Abs (move));
         //tells the Speed variable in the animation controller that speed is this.
 
         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
         //tells the vertical speed variable in the animation controller that speed is this.
 
         if (!grounded)
         {
             //return;
         }
 
         //rigidbody2D.velocity.y is the speed of up and down
         
         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
 
         
         if (move > 0 && !facingRight)
         {
             Flip ();
         }
         else if (move < 0 && facingRight)
         {
             
             Flip ();
         }
         //this is to keep you from switching direction when you don't want to
     }
 
     void Update()
     {
 
         //TestFixedUpdate ();
 
         //so we can get the jump value more accurately
         if ((grounded || !doubleJumped) && Input.GetButtonDown("Jump"))
         {
             anim.SetBool("Ground", false);
             //rigidbody2D.
             rigidbody2D.AddForce(new Vector2(0, jumpForce));
 
             if ((!doubleJumped))
             {
                 rigidbody2D.velocity = new Vector2(0, 0);
                 doubleJumped = true;
             }
         }
     }
 
     void Flip()
     {
         facingRight = !facingRight;
 
         Vector3 theScale = playerGraphics.localScale;
         Vector3 theScaleArm = armGraphics.localScale;
         //gets the local scale
         theScale.x *= -1;
         //turns around the scale
         //theScaleArm.y *= -1;
 
         playerGraphics.localScale = theScale;
         //sets the new scale
 
         armGraphics.localScale = theScaleArm;        
 
         Vector3 armTransform = armGraphics.localPosition;
         armTransform.x *= -1;
         armGraphics.localPosition = armTransform;
     }
 
 }


 using UnityEngine;
 using System.Collections;
 
 public class ArmRotator : MonoBehaviour {
     public int rotationOffset = 0;
 
     
     Animator anim;
     bool flippedArm;
     bool faceRight;
     void Start () {
         anim = GetComponentInParent<Animator>();
     }
     // Update is called once per frame
     void FixedUpdate () {
         
         faceRight = GetComponentInParent<CharacterControllerScript> ().facingRight;
         //subtracting the position of the player from the mouse position
 
         Vector3 difference;
         difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;    
 
 
 
 
         difference.Normalize ();        //normalizing the vector. the vector will be 1.
         
         float rotZ = Mathf.Atan2 (difference.y, difference.x) * Mathf.Rad2Deg;    //find the angle in degrees
 
 
         transform.rotation = Quaternion.Euler (0f, 0f, rotZ + rotationOffset);
 
 
         
         if (!flippedArm && ((rotZ >90) || (rotZ <-90)))
         {
             FlipArm();
         }
         else if ((rotZ <90) && flippedArm && (rotZ >-90) )
         {
             FlipArm();
         }
 
 
         
         if (!faceRight)
         {
             rotZ = Mathf.Abs(rotZ-180);
             if (rotZ>180)
                 rotZ -= 360;
         }
         anim.SetFloat ("Angle_Arm", rotZ);
 
     }
         
         void FlipArm()
         {
         Vector3 theScale = transform.localScale;
 
             flippedArm = !flippedArm;
         
             theScale.y*= -1;
 
         
             transform.localScale = theScale;
         }
 }
 



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

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

28 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

Related Questions

How to call sprite at run time 3 Answers

Same sprite animation on multiple objects 1 Answer

How to increase sprite animation speed 2 Answers

Sprite animation triggers OnCollisionEnter2D every frame (no root motion or animate physics) 0 Answers

Playing two sprite-based animations simultaneously (Not Legacy) 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