I have 2 scripts. Script #1 makes the Ai move in a certain path and the second one its the interaction with the player. What i want him to do is when the player walks into the sphere collider of the ai, he stops his movement from the path script, but i dont know hwo to
Path script:
using UnityEngine;
using System.Collections;
public class pathdragon : MonoBehaviour {
public Transform[] Waypoints;
public float Speed;
public int curWayPoint;
public bool doPatrol= true;
public Vector3 Target;
public Vector3 MoveDirection;
public Vector3 Velocity;
public Rigidbody pathDragonbody;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
pathDragonbody = GetComponent<Rigidbody> ();
if (curWayPoint < Waypoints.Length) {
Target = Waypoints [curWayPoint].position;
MoveDirection = Target - transform.position;
Velocity = pathDragonbody.velocity;
if (MoveDirection.magnitude < 1) {
curWayPoint++;
} else {
Velocity = MoveDirection.normalized * Speed;
}
} else {
if(doPatrol){
curWayPoint = 0;
}
}
pathDragonbody.velocity = Velocity;
transform.LookAt (Target);
}
}
Script #2:
using UnityEngine;
using System.Collections;
public class DragonScript : MonoBehaviour {
public float fpsTargetDistance;
public float enemyLookDistance;
public float attackDistance;
public float enemyMovementSpeed;
public float damping;
public Transform fpsTarget;
public Rigidbody Dragonbody;
Renderer myRender;
public float thrust;
pathdragon path;
// Use this for initialization
void Start () {
myRender = GetComponent<Renderer> ();
Dragonbody = GetComponent<Rigidbody> ();
path = GetComponent<pathdragon> ();
}
// Update is called once per frame
void FixedUpdate () {
fpsTargetDistance = Vector3.Distance (fpsTarget.position, transform.position);
if (fpsTargetDistance < enemyLookDistance) {
path.enabled = false;
myRender.material.color = Color.yellow;
lookAtPlayer ();
//print ("look at player please");
} else {
path.enabled = true;
path.Speed = 10f;
}
if (fpsTargetDistance < attackDistance) {
myRender.material.color = Color.red;
attackPlease ();
//print ("ATTACK!");
} else {
myRender.material.color = Color.blue;
}
}
void lookAtPlayer(){
Quaternion rotation = Quaternion.LookRotation(fpsTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime*damping);
}
void attackPlease(){
Dragonbody.AddForce (transform.forward * enemyMovementSpeed);
}
}