Quantcast
Viewing latest article 1
Browse Latest Browse All 14

Unity AI Questions (learning project)

I am working on a game where I need artificial intelligence for enemy characters to, A: Stay close enough to the ground, B: Generate Patrol Lines Between Several Waypoints, C: Be Able to Generate Patrol Lines while chasing the player, that avoids obstacles. I know about all of the packages and stuff, but I just need some insight on these concepts, not someone to write scripts for me. I'm an ok programmer, but not a "newbie" despite my name. The reason I want to create my own is so that I have full control over the AI and this is a learning project, so getting an already made set of scripts which I cannot really customize and won't learn from, won't help. If I get help with the above listed concepts, I'll already understand how to implement new things. Here is my current script (some of it is recycled from my older scripts): var rotationSpeed = 5; var moveSpeed = 5; var maxHealth = 100.0; var curHealth = 100.0; var healthBarLength : float; var waypoint : Transform[]; var currentWaypoint : int; var target : Transform; var groundDistance : float; var isGrouned : boolean; function Start(){ healthBarLength = Screen.width / 4; distToGround = collider.bounds.extents.y; } function Update(){ var targetPos = target.position; targetPos.y = transform.position.y; var targetRotation = Quaternion.LookRotation(targetPos - transform.position); var i = Time.deltaTime * rotationSpeed; var m = Time.deltaTime * moveSpeed; if(Physics.Raycast(transform.position, -Vector3.up, groundDistance)){ isGrounded = true; }else isGrounded = false; if(isGrounded){ transform.rotation = Quaternion.Slerp( transform.rotation, targetRotation, i); transform.position = Vector3.MoveTowards(transform.position, target.position, m); } } function AdjustCurrentHealth() { if(curHealth < 0) curHealth = 0; if(curHealth > maxHealth) curHealth = maxHealth; if(maxHealth < 1) maxHealth = 1; healthBarLength = (Screen.width / 4) * (curHealth / maxHealth); } function OnGUI() { GUI.Box(Rect(1050, 10, healthBarLength, 20), curHealth + "/" + maxHealth); } function OnDrawGizmos () { // Draws a 5 meter long red line in front of the object Gizmos.color = Color.red; var direction : Vector3 = transform.TransformDirection (Vector3.forward) * 10; Gizmos.DrawRay (transform.position, direction); var direction2 : Vector3 = transform.TransformDirection (-Vector3.up) * groundDistance; Gizmos.DrawRay (transform.position, direction2); }

Viewing latest article 1
Browse Latest Browse All 14

Trending Articles