I have been try to find and try the code from google and unity3d.com, but still cannot work, maybe i dun understand.
The code i using now is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GuardMovement : MonoBehaviour
{
//public float speed;
Rigidbody2D rb;
private Animator animator;
protected Vector3 velocity;
public Transform _transform;
public float distance = 5f;
public float speed = 1f;
Vector3 _originalPosition;
bool isGoingLeft = false;
public float distFromStart;
/*private float movespd = 1.0f;
//private int dir = -1;
public const float TurnLimit = 3.5f, CheckLimit = 2.0f;
public bool detect_player = false;
public bool after_chase = false;
public bool checkplayerloc = false;
public bool dirCheck = false;
public bool chgdir = false;
public enum State{Patrol, ChasePlayer, ReturnToOriloc};
public State behaviourState;
private Rigidbody2D enemy_rb2d;
private Vector3 oriloc;
private Vector2 newposition;
//private float checktime = CheckLimit;
//turntime = TurnLimit,*/
private Transform player;
void Start ()
{
rb = GetComponent ();
animator = this.GetComponent ();
_originalPosition = gameObject.transform.position;
_transform = GetComponent();
velocity = new Vector3(speed,0,0);
_transform.Translate ( velocity.x*Time.deltaTime,0,0);
player = GameObject.FindGameObjectWithTag ("Player").transform;
//oriloc = transform.position;
//behaviourState = State.Patrol;
//enemy_rb2d = gameObject.GetComponent ();
}
void Update ()
{
distFromStart = transform.position.x - _originalPosition.x;
if (isGoingLeft)
{
// If gone too far, switch direction
if (distFromStart < -distance)
SwitchDirection ();
_transform.Translate (-velocity.x * Time.deltaTime, 0, 0);
if (distFromStart < 3)
{
animator.SetInteger ("Direction", 3);
}
}
else
{
// If gone too far, switch direction
if (distFromStart > distance)
SwitchDirection ();
_transform.Translate (velocity.x * Time.deltaTime, 0, 0);
if (distFromStart > -3)
{
animator.SetInteger ("Direction", 1);
}
}
}
void SwitchDirection()
{
isGoingLeft = !isGoingLeft;
//TODO: Change facing direction, animation, etc
}
}
How do i put the code inside my code that the enemy have chase my character in a certain distance, after that distance enemy will go back to it own certain area continuous walk around in that area.
↧