Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Flip sprite in unity

/*
**Beware; fliping a character using the sprite renderer won't flip important components like colliders attached to it
**To Correctly flip a character, Flip the character by changing the y rotation or by changing the scale of the x axis to -1 or 1
*/

///Fliping a character in Unity 2D
	//Declare variables
	bool _facingRight

	void Update()
	{
		if (Input.GetAxisRaw("Horizontal") > 0  &&  !_facingRight)
        {
            Flip();
        }
        
        //
   		else if (_moveHorizontal < 0 && _facingRight )
        {
            //calls the flip method
            Flip();
        }
 	}   
  
	//Flip Method 
    void Flip()
    {

        //flips the character along the x axis using the sprite renderer correspondingly
        gameObject.GetComponent<SpriteRenderer>().flipX = !gameObject.GetComponent<SpriteRenderer>().flipX;
        
        //Sets the facingright bool to the opposite of what its currently at to prevent repeated flipping
        _facingRight = !_facingRight;
        
        //Flips the character by Rotating the character 180 degrees along the y axis
        //gameObject.GetComponent<Rigidbody2D>().transform.Rotate(0, 180, 0);
        
        //Flips the character by the changing the scale of the x axis to 1 or minus 1
        //gameObject.GetComponent<Rigidbody2D>().transform.localScale = new Vector3(transform.localScale.x*-1, transform.localScale.y, transform.localScale.z);
        
        
    }
Comment

unity c# flip sprite

   public Camera cam;
   public Transform aimTransform;



Vector3 mousePosition = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y));
        Vector3 aimDirection = (mousePosition - transform.position).normalized;
        float angle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg;
        aimTransform.eulerAngles = new Vector3(0, 0, angle);
Comment

PREVIOUS NEXT
Code Example
Csharp :: npm install --save vue-route@n 
Csharp :: unity ui movement 
Csharp :: c# Get all class by namespace 
Csharp :: how return only value of array in laravel 
Csharp :: array to list c 
Csharp :: c# find substring in string 
Csharp :: how to create a random vector2 in unity 
Csharp :: raycasting in unity 
Csharp :: c# contains() 
Csharp :: c# set datetime to null value 
Csharp :: asp.net core get previouse url 
Csharp :: c# OrderBy desc 
Csharp :: weapon switching unity 
Csharp :: roman to int 
Csharp :: unity rewarded ads 
Csharp :: wpf messagebox result 
Csharp :: unity deactivate component 
Csharp :: valid URL check in c# 
Csharp :: convert xml string to file c# 
Csharp :: lcm of numbers 
Csharp :: c# set cursor pos 
Csharp :: get what week of the month c# 
Csharp :: file to byte array 
Csharp :: asp.net core miniprofiler 
Csharp :: use slider in unity 
Csharp :: wpf get function name 
Csharp :: c# switch statements 
Csharp :: unity toint 
Csharp :: c# int 
Csharp :: c# implement ienumerable t 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =