How to move and FLIP a 2D sprite with the arrow keys
How to move and flip a 2D character with scripting (C#) in Unity 2018, using the left and right arrows keys to move the player. Unity beginner tutorial.
❤️ Subscribe to Oxmond Tutorials:
https://bit.ly/SubscribeOxmondTutorials
✅ Download the free Sunny Land 2D assets here:
https://assetstore.unity.com/packages/2d/characters/sunny-land-103349
✅ Other free packages at the Unity Assets Store:
https://assetstore.unity.com/lists/top-free-packages-13201
😷👕 Need a face mask / developer T-shirt? Drop by our shop and get a 20% DISCOUNT on your first purchase by using the discount code OXMONDSALE. Click here:
https://shop.oxmond.com/discount/OXMONDSALE
● Character script (from video):
/* .-------. .--. .-------. .--. .--. .--. | |--.--.--------.-----.-----.--| | |_ _|--.--| |_.-----.----|__|---.-| |-----. | - |_ _| | _ | | _ | | | | | | _| _ | _| | _ | |__ --| |_______|__.__|__|__|__|_____|__|__|_____| |___| |_____|____|_____|__| |__|___._|__|_____| © OXMOND / www.oxmond.com */ using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { void Update () { // Move the Character: transform.Translate(Input.GetAxis("Horizontal")* 15f * Time.deltaTime, 0f, 0f); // Flip the Character: Vector3 characterScale = transform.localScale; if (Input.GetAxis("Horizontal") < 0) { characterScale.x = -10; } if (Input.GetAxis("Horizontal") > 0) { characterScale.x = 10; } transform.localScale = characterScale; } }
● Character script (updated):
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { Vector3 characterScale; float characterScaleX; void Start() { characterScale = transform.localScale; characterScaleX = characterScale.x; } void Update () { // Move the Character: transform.Translate(Input.GetAxis("Horizontal") * 15f * Time.deltaTime, 0f, 0f); // Flip the Character: if (Input.GetAxis("Horizontal") < 0) { characterScale.x = -characterScaleX; } if (Input.GetAxis("Horizontal") > 0) { characterScale.x = characterScaleX; } transform.localScale = characterScale; } }
Hi, Thank you for making this tutorial, I’ve been searching for something like this for ages, so thank you. I have a question the code works perfectly but when I test it my character scales up massively is there a way to make it so that the character stays the same size as it originally was?
I look foward to hearing from you
Thanks again for the tutorial
Hannah
Hi Hannah,
Sorry for that. It’s just me doing some very lazy coding. In the video, I scale the character to 10, and in the code I just reverse that with the lines: characterScale.x = 10 & characterScale.x = -10;
A better way to do this is to define the characterScale first. Like this:
characterScale = transform.localScale;
characterScaleX = characterScale.x;
and then do the flipping:
characterScale.x = -characterScaleX;
etc.
I’ve updated the script above, so it should work like a charm now 🙂
.:Hans
could you add to jump?
pls i want to create a game .
Hi. Did you take a look at the jump tutorial?
https://oxmond.com/how-to-make-a-your-character-jump/
Hello thank you so much for this tutorial. I have a question about my script. The code works, but when I land on my platform my transform rotation x value changes and it makes my player bigger.
Here’s my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character2DController : MonoBehaviour
{
public float MovementSpeed = 1;
public float JumpForce = 1;
Vector3 characterScale;
float characterScaleX;
private Rigidbody2D _rigidbody;
private void Start()
{
_rigidbody = GetComponent();
characterScale = transform.localScale;
characterScaleX = characterScale.x;
}
private void Update()
{
var movement = Input.GetAxis(“Horizontal”);
transform.Translate(Input.GetAxis(“Horizontal”) * 15f * Time.deltaTime, 0f, 0f);
if (Input.GetButtonDown(“Jump”) && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
{
_rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
}
if (Input.GetAxis("Horizontal") 0)
{
characterScale.x = characterScaleX;
}
transform.localScale = characterScale;
}
}
i put script on player it look the oppisite way.
Hello There. I found your blog using msn. This iss an extremely well written article.
I’ll make sure to bookmark it and return to read more of your useful
information. Thanks for the post. I’ll definitely return.
I do not even know the way I stopped up here, but I believed this publish was once great. I don’t realize who you’re but certainly you are going to a famous blogger for those who aren’t already 😉 Cheers!
I was recommended this website by way of my cousin. I am no longer certain whether this post is written through him as no one else recognize such unique approximately my problem. You are incredible! Thanks!
Woah! I’m really enjoying this site. It’s simple, yet effective. I must say you have done a fantastic job with this. In addition, the blog loads extremely fast for me on Internet explorer. Excellent Blog!
Generally I do not learn post on blogs, however I would like to say that this tutorial very pressured me to try and do it! Thanks, quite nice post
Extremely useful information particularly the last part 🙂 I care for such information a lot. I was seeking this certain information for a very long time. Thank you and best of luck.