So my enemy is moving gust fine but i was wondering how to make his sprite flip right when he goes right and flip left when he goes left, right now he just faces left and stays there, please help thanks!
using UnityEngine;
using System.Collections;
public class Walker : MonoBehaviour {
public float walkSpeed = 2.0f;
public float wallLeft = 0.0f;
public float wallRight = 5.0f;
float walkingDirection = 1.0f;
Vector3 walkAmount;
// Update is called once per frame
void Update () {
walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
if (walkingDirection > 0.0f && transform.position.x >= wallRight)
walkingDirection = -1.0f;
else if (walkingDirection < 0.0f && transform.position.x <= wallLeft)
walkingDirection = 1.0f;
transform.Translate(walkAmount);
}
}
Instead of adjusting the moving direction you can rotate the object, this takes care of both where it’s going and where it’s facing. Depending on what you’re doing, inverting the scale can result in issues with child objects.
using UnityEngine;
using System.Collections;
public class Testing : MonoBehaviour
{
public float walkSpeed = 2.0f;
public float wallLeft = 0.0f;
public float wallRight = 5.0f;
Vector3 walkAmount;
// Update is called once per frame
void Update()
{
walkAmount.x = walkSpeed * Time.deltaTime;
if (transform.position.x >= wallRight && transform.rotation.eulerAngles.y <= 0)
{
transform.Rotate(Vector3.up, 180);
}
else if (transform.position.x <= wallLeft && transform.rotation.eulerAngles.y >= 180)
{
transform.Rotate(Vector3.up, -180);
}
transform.Translate(walkAmount);
}
}