How can I get the player to face the direction it is going in a Unity 2D Game?,How do I make it so that my character faces in the direction it is facing in a 2D Unity Game
Hi. How can I get the player to face the direction it is going in a Unity 2D Game?
I have looked all over the Internet for an answer, and I can't find anything that works.
The code I am using is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
Rigidbody2D body;
Transform target;
float horizontal;
float vertical;
float moveLimiter = 0.7f;
private bool facingRight;
public float runSpeed = 20.0f;
void Start ()
{
body = GetComponent<Rigidbody2D>();
}
void Update()
{
// Gives a value between -1 and 1
horizontal = Input.GetAxisRaw("Horizontal"); // -1 is left
vertical = Input.GetAxisRaw("Vertical"); // -1 is down
}
void FixedUpdate()
{
if (horizontal != 0 && vertical != 0) // Check for diagonal movement
{
// limit movement speed diagonally, so you move at 70% speed
horizontal *= moveLimiter;
vertical *= moveLimiter;
}
body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
}
}
If anybody could help me, it would be much appreciated.
Thanks. :)
,Hi. How do I make it so that my character faces in the direction it is facing in a 2D Unity Game?
I have looked across the internet for ages now, and I can't find anything that works.
The code I am using is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
Rigidbody2D body;
Transform target;
float horizontal;
float vertical;
float moveLimiter = 0.7f;
private bool facingRight;
public float runSpeed = 20.0f;
void Start ()
{
body = GetComponent<Rigidbody2D>();
}
void Update()
{
// Gives a value between -1 and 1
horizontal = Input.GetAxisRaw("Horizontal"); // -1 is left
vertical = Input.GetAxisRaw("Vertical"); // -1 is down
}
void FixedUpdate()
{
if (horizontal != 0 && vertical != 0) // Check for diagonal movement
{
// limit movement speed diagonally, so you move at 70% speed
horizontal *= moveLimiter;
vertical *= moveLimiter;
}
body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
}
}
If anyone could help, that would be great. :)
Thanks.
Your answer
Follow this Question
Related Questions
How can I get the player to face the direction it is going in a Unity 2D Game? 0 Answers
How to move a game object to a position after selecting it 0 Answers
How do I keep enemies (rigidbodies) from pushing each other? 1 Answer
Shooting an instantiated missile the way a object is facing. 0 Answers
How to SHOOT an object on a curved path without using Rigidbody 2 Answers