Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how to make a character run 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

how to make a character run in unity

 //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 :: split string c# 
Csharp :: unity gui text 
Csharp :: find how many digits a number has csharp 
Csharp :: check two lists are equal c# 
Csharp :: add a dictionary to another dictionary c# 
Csharp :: c# object list attribute to string 
Csharp :: unity image 
Csharp :: asp.net mvc image upload 
Csharp :: minimize maximize restore wpf buttons 
Csharp :: an existing connection was forcibly closed by the remote host. .net core 
Csharp :: c# datagridview select row right click 
Csharp :: how to set foreground from code wpf 
Csharp :: convert decimal to 2 decimal places c# 
Csharp :: c# generate guid from hash 
Csharp :: while c# 
Csharp :: group by ef core 
Csharp :: c# string to int 
Csharp :: get selected item datagrid wpf 
Csharp :: list min and Max value in c# 
Csharp :: c# allowedusernamecharacters 
Csharp :: Sort ListBox numerically in C# 
Csharp :: change sprite color unity 
Csharp :: unity c# cos inverse 
Csharp :: entity framework delete record with foreign key constraint 
Csharp :: to list c# 
Csharp :: on collision unity 
Csharp :: callling class c# 
Csharp :: how to keep rigidbody2D upright unity 
Csharp :: interface property implementation c# 
Csharp :: c# unit test for throwing exception method 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =