Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

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

unity 2d

/* How to scale any asset to your screen size:
Set up:
- Click on your asset or sprite in the project folder and change the
	'Pixels per Unit' from the default 100, to the resolution of the sprite.
    (e.g. 64, 128, 256, etc.) Then click 'Apply' at the bottom right.
- Attach the first script to the main camera (or an object that doesn't 
	effect the world).
- Attach the second script to the asset/sprite you want to scale.
*/

// First script
using UnityEngine;
public class ScreenSize : MonoBehaviour
{
    public static float GetScreenToWorldHeight
    {
        get
        {
            Vector2 topRightCorner = new Vector2(1, 1);
            Vector2 edgeVector = Camera.main.ViewportToWorldPoint(topRightCorner);
            var height = edgeVector.y * 2;
            return height;
        }
    }
    public static float GetScreenToWorldWidth
    {
        get
        {
            Vector2 topRightCorner = new Vector2(1, 1);
            Vector2 edgeVector = Camera.main.ViewportToWorldPoint(topRightCorner);
            var width = edgeVector.x * 2;
            return width;
        }
    }
}

// Second script
using UnityEngine;
public class Scaler : MonoBehaviour
{
	void Start()
	{
    	float width = ScreenSize.GetScreenToWorldWidth;
    	transform.localScale = Vector3.one * width;
	}
}

//=============================================================================
// Edit: After messing around with this a bit, this is a way to automatically
//		 fit a sprite to the monitors X and Y screen dimensions.

// Second script (NEW)
using UnityEngine;
public class Scaler : MonoBehaviour
{
	void Start()
	{
    	// Declare width and height values
    	float width = ScreenSize.GetScreenToWorldWidth;
    	float height = ScreenSize.GetScreenToWorldHeight;
        
        // Check if the window is landscape
        if (width > height)
        {
            transform.localScale = Vector3.one * height;
        }

		// Check if the window is portrait
        if (height > width)
        {
            transform.localScale = Vector3.one * width;
        }
        
        // Check if the length of the window edges are equal
        if (width == height)
        {
            transform.localScale = Vector3.one * ((width + height) / 2);
        }
	}
}

// Sorry for the long answer ;)
Comment

Unity 2d

void FixedUpdate()
{
    //Use Velocity
	if (Input.GetKey(KeyCode.D))
        {
        	//add velocity to rigidbody 3 units along x axis and 0 units along y axis
            //Velocity is not affected by Gravity
            //Velocity i constant in every frame I.e Force=3
            //Velocity starts from the initial value stated which in this case is 3
            //rigidbody2d.velocity = new vector2(x,y);
            //x is sideways and y is upwards
            
            rb2d.velocity = new Vector2(3,0);
            
            
           //To gradually increase Velocity in each frame see the line of code below
           
           rb2d.velocity += new Vector2(3,0);
        }
        
	//OR Use Force
	if (Input.GetKey(KeyCode.D))
        {
        	//Gradually adds Force/velocity to rigidbody 4 units to the right along x axis
            //Force is affected by gravity and Drag
            //Force starts from 0 and increases gradually by 4 units per frame
            rb2d.AddForce (Vector2.right * 4);
        }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: convert object to httpcontent c# 
Csharp :: c# add button to messagebox 
Csharp :: multi line comment c# 
Csharp :: multiplication using arrays 
Csharp :: c# binding add combobox with enum values 
Csharp :: speedtest.net cli 
Csharp :: How to make game object transparent in unity 
Csharp :: c# windows forms open directory in explorer 
Csharp :: c# enum 
Csharp :: array sort C Sharp 
Csharp :: c# randize list 
Csharp :: convert-integer-to-binary-in-c-sharp 
Csharp :: how to check if the value is alphabet and numbers only only in c# 
Csharp :: c# switch case greater than 
Csharp :: c# streamwriter add new line 
Csharp :: C# clear form 
Csharp :: unity banner Ad position 
Csharp :: c# how to print 
Csharp :: ray casting unity 
Csharp :: all substrings of a string c# 
Csharp :: c# debug writeline 
Csharp :: scale between tow ranges c# 
Csharp :: c# how to call methods from another class 
Csharp :: unity switch to scene and transfer data 
Csharp :: c# chunk array linq 
Csharp :: c# how to compare 2 dates without time 
Csharp :: set the page that FormsAuthentication.RedirectFromLoginPage redirects to 
Csharp :: c# singleton 
Csharp :: c# unit test for throwing exception method 
Csharp :: c# destroy function...unity 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =