Hello Unity! I was wondering how I would use the RAIN AI to make one of my enemies run away from the player when the player is a certain distance away from the enemy. If I cannot do this with RAIN, is there some other software I should get?
I think, You can do it with little script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RunAway : MonoBehaviour {
//player
GameObject player;
//distance, where will start to run
public float dist;
Rigidbody2D rb;
public float speed;
void Start () {
//find player in the scene_ player should have tag Player
player = GameObject.FindWithTag ("Player");
rb = GetComponent <Rigidbody2D> ();
}
void FixedUpdate () {
//calculate direction vector to player
Vector3 dir = player.transform.position - transform.position;
//check distance to player
if (dir.magnitude < dist) {
//run away
//es axample physic moving in oposite direction
rb.AddForce (-dir * speed);
}
}
}
here small example (unity 5.5.3, I think should be ok for 5.6 too)
3060202–229881–Run.unitypackage (7.99 KB)
OMG Dude!! Thanks a ton!! This is perfect!