/*
**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);
}
/* 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 ;)
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);
}
}