- Home /
.move dose not work with AI C#
This is my first game, and I am having issues with my first attacking AI. The best AI code I could find uses controller.move, but when I did it they did nothing. I did declare controller, and my AI dose have a character controller on it, but that dose nothing. I even un collided everything in the world. I also tried transform.translate, that got it moving, but it moved through walls.
using UnityEngine;
using System.Collections;
public class FlyingAI : MonoBehaviour {
Vector3 moveDirection = Vector3.zero;
float moveingsideways = 0;
float moveingforwards = 0;
float rising = 0;
float gravatyy = 6;
static public bool awakee = false;
Vector3 Moveing;
public float Heath = 10;
public float Maxhightt = 1;
public Rigidbody DeadScarab;
public Transform LookAtTarget;
Quaternion startingrotat;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//find the controller
CharacterController controller = GetComponent<CharacterController>();
moveingsideways = 0;
moveingforwards = 0;
rising = 0;
if (LookAtTarget.position.x + 18 > transform.localPosition.x){
if (LookAtTarget.position.x - 18 < transform.localPosition.x){
if (LookAtTarget.position.z - 18 < transform.localPosition.z){
if (LookAtTarget.position.z + 18 > transform.localPosition.z){awakee = true;}}}}
if (LookAtTarget.position.x + 18 < transform.localPosition.x) {awakee = false;}
if (LookAtTarget.position.x - 18 > transform.localPosition.x) {awakee = false;}
if (LookAtTarget.position.z - 18 > transform.localPosition.z) {awakee = false;}
if (LookAtTarget.position.z + 18 < transform.localPosition.z) {awakee = false;}
if (awakee)
{
if (LookAtTarget.position.x - 2 > transform.localPosition.x){ moveingsideways += 5;}
if (LookAtTarget.position.x + 2 < transform.localPosition.x){ moveingsideways -= 5;}
if (LookAtTarget.position.z + 2 < transform.localPosition.z){ moveingforwards -= 5;}
if (LookAtTarget.position.z - 2 > transform.localPosition.z){ moveingforwards += 5;}
if (transform.localPosition.y < 1)
{
rising = 7;
}
if (transform.localPosition.y > 1)
{
rising = 5;
}
if (transform.localPosition.y == 1)
{
rising = 6;
}
float totalfalling = rising - 6;
Moveing.x = moveingsideways;
Moveing.z = moveingforwards;
Moveing.y = totalfalling;
controller.SimpleMove(Moveing * Time.deltaTime);
if (transform.rotation.x != 0){ transform.localRotation = new Quaternion(0,0,0,0);}
if (transform.rotation.y != 0){ transform.localRotation = new Quaternion(0,0,0,0);}
if (transform.rotation.z != 0){ transform.localRotation = new Quaternion(0,0,0,0);}
}
}
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "Bullet")
{ Heath -= 5; }
if (Heath <= 0)
{
Rigidbody Falling;
Falling = Instantiate(DeadScarab,transform.localPosition,transform.rotation)as Rigidbody;
Destroy(gameObject);}
//DeadScarab
}
//outside code
}
Answer by Imankit · Feb 05, 2013 at 06:14 AM
OnCollisionEnter will not work on Character Controller
You have to take
void OnControllerColliderHit(ControllerColliderHit collision) {
if (collision.gameObject.tag == "Bullet")
{
Heath -= 5;
}
if (Heath <= 0)
{
Rigidbody Falling;
Falling = Instantiate(DeadScarab,transform.localPosition,transform.rotation)as Rigidbody;
Destroy(gameObject);}
//DeadScarab
}
//outside code
}
That made them immortal D: but they still did not move, so they were not that scary... It dose give me a new, fun error tho, "OnColisionEnter: script error" or something like that.
$$anonymous$$ay tou haven't removed your OnCollision function... Is this script assigned to a character controller?
If you want your AI to follow your Player then just LookAt your player and transverse in the forward direction of your AI
this is the script to get the enemy moving. The ai$$anonymous$$g at player is another script :D
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
AI scripting Question. 2 Answers
Multiple Cars not working 1 Answer
AI Field of vision 1 Answer
Make player not be seen by AI, when player in foilage and shadows. 1 Answer