- Home /
Flipping an enemy sprite and stop
the enemy sprite wont flip when it changes direction in a 2D environment, i tried using this first code. the monodelvelopment editor says it doesn't know what flip is. i got the code from a character controller, but my question is am i useing it totally wrong? O_o
void FixedUpdate (){
float move = Input.GetAxis ("Horizontal");
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 && ! facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
also I need the enemy ai to make the enemy stop when it gets to the player, or at a distance of something like 2. i have the "agro" side done. any tips on where to go? its a 2D platformer.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int maxDistance;
public int rotationSpeed;
public Transform myTransform;
bool facingRight = true;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxDistance = 15;
}
void update (){
}
void FixedUpdate () {
if (Vector3.Distance (target.position, myTransform.position) < maxDistance) {
if (target.position.x < myTransform.position.x)
myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; // player is left of enemy, move left
else if (target.position.x > myTransform.position.x)
myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; // player is right of enemy, move right
}
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
}
}
Answer by darthtelle · May 02, 2014 at 10:53 AM
Is your first block of code copied directly out of your editor? Are you missing a bracket closing FixedUpdate()? Because to me it looks like you've placed Flip() inside FixedUpdate(), but maybe I'm missing some of the code.
For your second issue, you should have another check inside your first distance check. Same thing, but if the distance is greater than whatever you want the offset to be, don't update the position. Pseudo code example.
float distance = Vector.Distance();
if((distance < maxDistance) && (distance > minDistance))
{
// Update movement
}
the first block was copied out of the editor, i was trying to show everything that was associated with those functions. i missed a few things >.> im trying my best to figure out how to make the sprite look the direction it is moving. tried the answers portion of this website but nothing they have works correctly.
second code:
do i need a separate parent If() function in FixedUpdate() ? or does it need to be part of the move function? I did this (the code below) and it gave me problems. (I defined $$anonymous$$Distance as 2; and made it a public int)
void FixedUpdate () {
if (Vector3.Distance ((target.position, myTransform.position) < maxDistance) && (target.position, myTransform.position) > $$anonymous$$Distance))
{
if (target.position.x < myTransform.position.x)
{
myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; // player is left of enemy, move left
}
else if (target.position.x > myTransform.position.x)
{
myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; // player is right of enemy, move right
}
}
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
}
Your answer
Follow this Question
Related Questions
Split Screen Camera - disable rendering? 1 Answer
How to extend a sprite 0 Answers
Steps to create 2D Animation from SpriteSheet 1 Answer
Legacy animations and 2D sprites Help! 0 Answers
Help! Weird Jerky Animation! 1 Answer