Skip to content

Instantly share code, notes, and snippets.

@fiskefyren fiskefyren/AI
Created Feb 23, 2016

Embed
What would you like to do?
// https://www.youtube.com/watch?v=UTLApMrHr5I
// https://www.youtube.com/watch?v=188SMf9f6UY <- this one is better?
using UnityEngine;
using System.Collections;
public class EasyAI : MonoBehaviour {
public float fpsTargetDistance;//how far away the player is from the enemy
public float enemyMovementSpeed;//how fast the enemy moves
public float turning;
public Transform fpsTarget;
Rigidbody theRigidbody;
Renderer myRender;
void Start() {
myRender = GetComponent<Renderer>();
theRigidbody = GetComponent<Rigidbody>();
isFiring = false;
}
void FixedUpdate() {
fpsTargetDistance = Vector3.Distance(fpsTarget.position, transform.position);//current this is only used to show distance between player and enemy
//myRender.material.color = Color.red;
lookAtPlayer();
chasePlayer();
shootPlayer();
}
void lookAtPlayer() {
Quaternion rotation = Quaternion.LookRotation(fpsTarget.position - transform.position);
//this is the actual rotation part
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turning * Time.deltaTime);
}
void chasePlayer() {
theRigidbody.velocity = transform.forward * enemyMovementSpeed * Time.deltaTime;
}
public float fireRate;
public float cooldown;
//checking for whether or not to fire
public bool isFiring = false;
//firing points transform for launching projectiles
public Transform leftFirePoint;
public Transform rightFirePoint;
//our projectile object
public GameObject laserPrefab;
public AudioSource fireFXSound;
void shootPlayer() {
RaycastHit rayHit;
if (Physics.Raycast(transform.position, transform.forward, out rayHit)) { //out is a keyword, you can add another var for the distance of the ray
isFiring = true;
/*
I'm clueless D:
*/
}
Debug.Log("Shooting at player!" + rayHit.collider.gameObject.tag);
}
GameObject.Instantiate(laserPrefab, leftFirePoint.position, leftFirePoint.rotation);
GameObject.Instantiate(laserPrefab, rightFirePoint.position, rightFirePoint.rotation);
cooldown = fireRate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.