Mesh issue:
AI has a RigidBody and a CharacterController.
Mesh only has a mesh collider.Mesh has a small bump/hill like adjustment in the middle, the AI slopes upwards fine, but at the top point they walk through it towards the player. AI also walks through walls with box colliders.
To stop AI going through walls/hills/meshes, is there an easy way without raycasting? If I must use raycasting, is there an easy tutorial because I get confused reading other people’s raycasting scripts.
If I can get the tagging script system working correctly for the sphere collider section below this section, then I only need to raycast/stop AI from walking through hills/etc on the Y axis I believe.
SphereCollider issue:
AI is using a sphere collider, if player moves into it then it’ll target the player and move towards. If player is out of range, AI moves back to spawn Position unless Player walks back within range.
Issue: If one enemy2 is following the player, and the player is inside this enemy2’s sphere collider, after the other enemy1 has walked back to spawn position, if enemy2’s sphere collider touches enemy1’s sphere collider, then enemy1 will activate towards the player.
Sphere collider script:
private var AIScript:AIMelee;
private var BackOff:AIReturnToSpawnPos;
function Awake() {
var sc : SphereCollider;
sc = this.gameObject.AddComponent ("SphereCollider");
sc.radius = 10;
sc.isTrigger = true;
AIScript = GetComponent(AIMelee);
AIScript.enabled = false;
BackOff = GetComponent(AIReturnToSpawnPos);
BackOff.enabled = false;
rigidbody.Sleep();
}
function OnTriggerEnter(other:Collider){
if(other.tag == "Player");
AIScript.enabled = true;
BackOff.enabled = false;
}
function OnTriggerExit(other:Collider){
if(other.tag == "Player");
AIScript.enabled = false;
BackOff.enabled = true;
}
AI Enemy2’s script:
var TargetPlayer : Transform; //Target Player
var Self : Transform; //Target AI's Self
var speed = 2.0; //Variable for speed
var lockPos = 0; //Variable for locking rotation position
var maxDistance = 1.5; //Maximum distance to move to at Player
var gravityForce = 20;
public var controller : CharacterController;
function Update() {
transform.LookAt(TargetPlayer.position); //Tells object to look at and move to the target's XYZ Position
if(Vector3.Distance(TargetPlayer.position, Self.position) > maxDistance) /*Keeps AI distance from Player*/ {
transform.position += speed * Time.deltaTime * transform.forward; //Mathematics for speed tells object to only move forward at target
controller.Move( Vector3(0, controller.velocity.y - gravityForce * Time.deltaTime, 0) * Time.deltaTime); //Applies Gravity to physic component char controller
transform.rotation = Quaternion.Euler(lockPos, lockPos, lockPos); //Stops object from rotating in all positions
transform.position.y=56.28055; //Stops object from moving upwards (jumping) in y position any further from set value
}
}
Enemy2’s BackOff (return to spawn position) code:
private var spawnPos : Vector3;
private var spawnRot : Quaternion;
var speed = 3.0; //Variable for speed
var lockPos = 0; //Variable for locking rotation position
private function Awake()
{
spawnPos = transform.position;
spawnRot = transform.rotation;
}
function Update ()
{
transform.LookAt(spawnPos);
transform.position += speed * Time.deltaTime * transform.forward;
transform.rotation = Quaternion.Euler(lockPos, lockPos, lockPos); //Stops object from rotating upwards in y position
}
Issue aside, I’m also thinking, if you can have one enemy off the sidelines to fight at a time, that it won’t provide the desired effect I want.
I’m not sure how to do this myself, and need a little help.
Lets say I put a cube with (isTrigger, mesh off) around where the enemies are, if the player walks within this cube, it activates all enemies to target player, if Player runs away, then all enemies return to position.
Please see the second edit of my second post on this thread in bold for an update on this section.
Thanks!