- Home /
Question by
iKamoficial · Feb 19, 2017 at 11:04 PM ·
2d game2d-platformer
Enemy facing moving direction.
Hi guys i'm trying to amek an enemy but when he moves he does;t face the moving directions and i have the enemy movement in one script and the facing in another how i do that?
Scripts here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyChaseing : MonoBehaviour {
public float moveSpeed;
private Rigidbody2D myRB;
private bool moving;
public float TimeBetweenMove;
private float timeBetweenMoveCounter;
public float TimeToMove;
private float timeToMoveCounter;
private Vector3 MoveDirection;
// Use this for initialization
void Start ()
{
myRB = GetComponent<Rigidbody2D> ();
//timeToMoveCounter = TimeToMove;
//timeBetweenMoveCounter = TimeBetweenMove;
timeBetweenMoveCounter = Random.Range (TimeBetweenMove * 0.75f, TimeBetweenMove * 1.25f);
timeToMoveCounter = Random.Range (TimeToMove * 0.75f, TimeBetweenMove * 1.25f);
}
// Update is called once per frame
void FixedUpdate ()
{
if (moving)
{
timeToMoveCounter -= Time.deltaTime;
myRB.velocity = MoveDirection;
if (timeToMoveCounter < 0f)
{
moving = false;
//timeBetweenMoveCounter = TimeBetweenMove;
timeBetweenMoveCounter = Random.Range (TimeBetweenMove * 0.75f, TimeBetweenMove * 1.25f);
}
}
else
{
timeBetweenMoveCounter -= Time.deltaTime;
myRB.velocity = Vector2.zero;
if (timeBetweenMoveCounter < 0f)
{
moving = true;
//timeToMoveCounter = TimeToMove;
timeToMoveCounter = Random.Range (TimeToMove * 0.75f, TimeBetweenMove * 1.25f);
MoveDirection = new Vector3 (Random.Range (-1f, 1f)* moveSpeed, 0f, 0f);
}
}
}
}
And second script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class StealthEnemyAI : MonoBehaviour {
//Enemy sight intels
public Transform sightStart,sightEnd;
public bool spotted = false;
public GameObject DetectedMark;
//Fliping intel
public bool facingleft = true;
void Start()
{
InvokeRepeating ("Patrol", 0f, Random.Range (2f, 6f));
}
void Raycasting ()
{
Debug.DrawLine (sightStart.position, sightEnd.position, Color.green);
spotted = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer ("Player"));
}
void FixedUpdate ()
{
Raycasting ();
Behaviours ();
}
void Behaviours()
{
if (spotted == true)
{
DetectedMark.SetActive (true);
} else
{
DetectedMark.SetActive (false);
}
}
void Patrol()
{
//Flip randomly
facingleft = !facingleft;
if (facingleft == true)
{
transform.eulerAngles = new Vector2 (0, 0);
}
else
{
transform.eulerAngles = new Vector2 (0, 180);
}
}
}
Please help me!
Comment
Your answer
Follow this Question
Related Questions
Flip Player, but don't flip the Player's Child. 2 Answers
Instantiate a GameObject with a specific Z rotation 2 Answers
Huge lag when toggling between Tile maps in one scene. 0 Answers
Problem with Jump 1 Answer
platform 2d about collider 3 Answers