- Home /
Rotating Player
Hey guys, I am making a Top Down Horror Game with one of my friends, and we are having difficulties flipping the player. In the transform.localScale = new Vector3(1f,1f,1f,) wouldn't that turn the player, and putting a negative would make him spin the other way? Thanks!
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float moveSpeed;
public float jumpSpeed;
private Rigidbody2D myRigidBody;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
public bool isGrounded;
private Animator myAnim;
public Vector3 respawnPosition;
public float speed = 0f;
private Rigidbody2D myRigidBody;
// Use this for initialization
void Start () {
myRigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetAxisRaw("Horizontal") > 0f)
{
myRigidBody.velocity = new Vector3(moveSpeed, myRigidBody.velocity.y, 0f);
transform.localScale = new Vector3(1f, 1f, 1f);
}
else if (Input.GetAxisRaw("Horizontal") < 0f)
{
myRigidBody.velocity = new Vector3(-moveSpeed, myRigidBody.velocity.y, 0f);
transform.localScale = new Vector3(1f, -1f, 1f);
}
else
{
myRigidBody.velocity = new Vector3(0f, myRigidBody.velocity.y, 0f);
}
if (Input.GetAxisRaw("Vertical") > 0f)
{
myRigidBody.velocity = new Vector3(myRigidBody.velocity.y, moveSpeed, 0f);
transform.localScale = new Vector3(1f, 1f, 1f);
}
else if (Input.GetAxisRaw("Vertical") < 0f)
{
myRigidBody.velocity = new Vector(myRigidBody.velocity.y, -moveSpeed, 0f);
transform.localScale = new Vector3(-1f, 1f, 1f);
}
else
{
myRigidBody.velocity = new Vector3(myRigidBody.velocity.y, 0f, 0f);
}
myAnim.SetFloat("Speed", Mathf.Abs(myRigidBody.velocity.x));
myAnim.SetBool("Grounded", isGrounded);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "KillPlane")
{
//gameObject.SetActive(false);
transform.position = respawnPosition;
}
if (other.tag == "Checkpoint")
{
respawnPosition = other.transform.position;
}
}
}
Comment