Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Moving AI away or towards player not working correctly.

$
0
0
What I am trying to do with the script below is to have the AI move towards the player once the player has entered the overlap sphere. But what happens instead is the AI moves away from the player. What is also weird is that if the player's X position is greater than zero the AI moves to the right only, if the X position is less than zero the AI moves left only. I've tried using Vector3.Lerp, it worked but I'd much rather use a character controller for movements. Anyways, here is the code. #pragma strict var health : float; var armor : float; var neutral = false; var vision_Range : int; var moveScript : AIMove; var rayLength : float; var mask : LayerMask; var targets : Transform = null; var controller : CharacterController; function Update(){ var cols : Collider[] = Physics.OverlapSphere(transform.position, vision_Range, 1<<9); //AI jumping if (Physics.Raycast(transform.position, Vector3.left, rayLength, mask)){ moveScript.Jump(); }else if (Physics.Raycast(transform.position, Vector3.right, rayLength, mask)){ moveScript.Jump(); } //AI jumping for (var col : Collider in cols){ targets = col.transform; } if (targets == null){ controller.Move(Vector3.zero); }else{ controller.Move(targets.position * Time.deltaTime * 4); } }

Viewing all articles
Browse latest Browse all 171066

Trending Articles