- Home /
there are no write-code-for-me questions on this site, substantial help already given
AI bug issue
Below is a semi intelligent ai script. It works, but it has an issue with the rotation. Locking the rigidbody's rotation x and z. (None of the units are meant to move up and down, as the entire game is set on a flat surface.) Anyway, the ai basically does a barrel roll and prevents the computer to from actually moving around an object.
In addition, locking the y position on the rigidbody for some reason also causes the ai to only look at the target, instead of following the target. The only other codes attached are a instantiating code to fire a projectile, and a code to animate the simple gun to fire.
The code is below, and any thoughts are appreciated.
#pragma strict
@script RequireComponent (Rigidbody)
private var myTransform : Transform;
private var myRigidbody : Rigidbody;
var target : Transform; // the player
var movespeed : float = 6.0;
var turnspeed : float = 1.0;
var rayDistance : float = 10;
var mp : float = .35;
private var desiredVelocity : Vector3;
var isGrounded : boolean = false;
function Start ()
{
myTransform = transform;
myRigidbody = rigidbody;
myRigidbody.freezeRotation = true;
}
function Update ()
{
Moving();
}
function Moving()
{
//rotation
var lookDirection : Vector3 = (target.position - myTransform.position).normalized;
var hit : RaycastHit;
var shoulderMultiplier : float = mp;
var leftRayPos : Vector3 = myTransform.position - (myTransform.right * shoulderMultiplier);
var rightRayPos : Vector3 = myTransform.position + (myTransform.right * shoulderMultiplier);
if (Physics.Raycast (leftRayPos, myTransform.forward, hit, rayDistance))
{
Debug.DrawLine (leftRayPos, hit.point, Color.red);
lookDirection += hit.normal * 1.0;
}
else if (Physics.Raycast (rightRayPos, myTransform.forward, hit, rayDistance))
{
Debug.DrawLine(rightRayPos, hit.point, Color.red);
lookDirection += hit.normal * 1.0;
}
else
{
Debug.DrawRay(leftRayPos, myTransform.forward * rayDistance, Color.yellow);
Debug.DrawRay(rightRayPos, myTransform.forward * rayDistance, Color.yellow);
}
var lookRot : Quaternion = Quaternion.LookRotation( lookDirection );
myTransform.rotation = Quaternion.Slerp (myTransform.rotation, lookRot, turnspeed * Time.deltaTime);
//movement
desiredVelocity = myTransform.forward * movespeed;
desiredVelocity.y = myRigidbody.velocity.y;
}
function FixedUpdate()
{
if(isGrounded)
{
myRigidbody.velocity = desiredVelocity;
}
}
function OnCollisionEnter (collision : Collision)
{
if ( collision.collider.gameObject.name == "Floor")
{
isGrounded = true;
}
}
function OnCollisionStay (collision : Collision)
{
if ( collision.collider.gameObject.name == "Floor")
{
isGrounded = true;
}
}
function OnCollisionExit (collision : Collision)
{
if ( collision.collider.gameObject.name == "Floor")
{
isGrounded = false;
}
}
It seems to work fine in a 3-dimensional environment : http://www.youtube.com/watch?v=02bXl1mBQXs&t=15m00s
I believe it was suitable for the purpose and audience it was written for, and with the aim of giving the viewer the tools to write their own AI code, to suit their specific purpose.
$$anonymous$$y thoughts are : this is a write-my-code question. I have already done that for you. The rest is up to you ....
The game is in a 3d world. The ai just isn't meant jump or go up in the y axis.
So what I'm asking is if I'm missing something in the code that's telling the bot that they can rotate in the x and z. If it isn't I'll comb over the rest of the asset in an attempt to find the problem. I'm not asking anyone to write my code.
If you click on the link provided in my first comment, you'll see the NPC moving in a 3-Dimensional environment. I can tell just by looking at the conditionals in your collision events that you havn't done all of my NPC videos, therefore you are asking questions about things you have yet to complete.
Follow this Question
Related Questions
Obstacle avoidance fine tuning assistance. 0 Answers
Implementing BUG2 algorithm 0 Answers
AI Field of vision 1 Answer
Using box collider to determine if AI should stop help 1 Answer
How can I draw a ray for direction object is moving? 1 Answer