How do I get my enemy to flip to face the direction he is moving?
I have searched quite a bit for this but can not seem to find exactly what I am looking for.
I have a script to make my enemy character move along the x axis between multiple different way points. What I cant figure out is how to get him to flip to face the direction he is moving.
My code is here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyManager : MonoBehaviour
{
public int maxHealth = 100;
int currentHealth;
public int destroyTime = 5;
public float speed = 1f;
private float waitTime;
public float startWaitTime;
public Transform[] wayPoints;
private int randomSpot;
public Animator anim;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
waitTime = startWaitTime;
randomSpot = Random.Range(0, wayPoints.Length);
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, wayPoints[randomSpot].position, speed * Time.deltaTime);
if (Vector2.Distance(transform.position, wayPoints[randomSpot].position) < 0.2f)
{
if (waitTime <= 0)
{
randomSpot = Random.Range(0, wayPoints.Length);
waitTime = startWaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
}
}
Answer by lgarczyn · Dec 21, 2019 at 07:18 PM
@andererdude is wrong, this will not work in 2D. The reason is that it will align the forward axis with the target, which means that the object will "leave" the camera plane, and be perpendicular to it. That usually means the object will disappear from the camera.
For a top-down view, you want:
transform.rotation = Quaternion.LookRotation(Vector3.forward, target - transform.position);
For a sidescroller, you want
transform.localScale.Set(target.x > transform.position.x ? 1f : -1f, 1f);
assuming the default orientation is right. target
is the position of the player/the enemy's target.
I am fairly new to program$$anonymous$$g in c# I am creating a sidescroller so I decided to use the bottom snippet, however I get two errors when I enter that code.
"Assets\Scripts\Enemy Scripts\Enemy$$anonymous$$anager.cs(30,9): error CS1612: Cannot modify the return value of 'Transform.localScale' because it is not a variable"
"Assets\Scripts\Enemy Scripts\Enemy$$anonymous$$anager.cs(30,34): error CS0103: The name 'target' does not exist in the current context"
Also the initial orientation of my enemy will be facing left.
Could you please elaborate on the code I would like to learn exactly what it is doing. So that I have a better understanding of it, I don't really want to just copy and paste things. Thank you.
Sorry, I forgot about the subtleties of c#, updated the code
So I tried updated code using randomSpot as the target, however I get this error.
"error CS1061: 'int' does not contain a definition for 'x' and no accessible extension method 'x' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)"
Answer by andererdude · Dec 21, 2019 at 01:20 AM
Hey there, what about
transform.LookAt(wayPoints[randomSpot]);
(before you begin to move) I am not sure what this does in 2D space, though :O
Answer by jihadkhawaja · Dec 21, 2019 at 01:28 AM
You can use a simple trick by changing the gameobject transform scale from 1 to -1 on the responsible axis which will flip the character.
When your character x value start decreasing change the transform x scale to -1 (could be the opposite which is 1), and when x position increase change scale back to 1
Your answer
Follow this Question
Related Questions
How To Stop Enemy Movement During Its Attack Animation 1 Answer
Enemy direction going crazy after collision 1 Answer
Fix for Enemy AI script 0 Answers
Move enemy only touching border 1 Answer