Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how to flip character in unity 2d

Put this in Update() = 

    //Flip the player's localScale.x 
    if the move speed is greater than .01 or less than -.01

        if (targetVelocity.x < -.01)
        {
            transform.eulerAngles = new Vector3(0, 180, 0); // Flipped
        }
        else if (targetVelocity.x > .01)
        {
            transform.eulerAngles = new Vector3(0, 0, 0); // Normal
        }
Comment

Flip a player in Unity 2D

/*
**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

How to flip a character in Unity 2D

 //Checks if the left and right keys are pressed and flips the player accordingly
 //Make sure the player scale on the x axis is 1
    
    //Checks if the right key is pressed
    if (moveHorizontal > 0)
   	 {
     	rb2D.transform.localScale = new Vector3(1, transform.localScale.y, transform.localScale.z);
   	 }

	//Checks if Left Key is pressed
	else if (moveHorizontal < 0)
  	 {
       rb2D.transform.localScale = new Vector3(-1, transform.localScale.y, transform.localScale.z);
  	 }
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to get an child of an gameobject 
Csharp :: unity log 
Csharp :: c# convert dictionary to anonymous object 
Csharp :: convert timestamp to datetime c# code 
Csharp :: how to move mouse in game c# 
Csharp :: c# random boolean 
Csharp :: exit application wpf 
Csharp :: c# resize image keep aspect ratio 
Csharp :: xamarin button text lowercase 
Csharp :: switch case c# range 
Csharp :: how to find the mouse position unity 
Csharp :: c# empty IEnumerable 
Csharp :: unity android back button 
Csharp :: how to access gameobject name 
Csharp :: wpf choose file dialog 
Csharp :: how to populate listbox using list<t c# 
Csharp :: c# string to datetime 
Csharp :: There is already a virtual axis named Horizontal registered. unity 
Csharp :: system.text.json DeserializeAsync when to use 
Csharp :: c# change cursor 
Csharp :: c# copy list without reference 
Csharp :: set ads for children admob unity 
Csharp :: wpf load file content 
Csharp :: add items to listbox from text file c# 
Csharp :: c# wirite to csv 
Csharp :: c# convert int to pretty string 
Csharp :: c# add guid to array 
Csharp :: unity instantiate 
Csharp :: unity 2d looka tt mouse 
Csharp :: how to check is object by this type c# 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =