AI help (492911)

i have a basic AI script but i need to avoid objects that it is not after Eg go round or over objects and i’m wondering if any one can help with is here’s the scripts so far

var target : Transform; //the enemy's target
var target2 : Transform;
var target3 : Transform;

var moveSpeed = 3; //move speed

var rotationSpeed = 3; //speed of turning

var myTransform : Transform; //current transform data of this enemy

function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
     target = GameObject.FindWithTag("pickup").transform; //target the player

}

function Update () {
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;


}

i think i may of posted this in the wrong section so just in case i have here’s the support link http://forum.unity3d.com/threads/162872-AI-help?p=1113389#post1113389

Greetings,

What you want to do, is to check if your object is colliding whit anything on its way, and if it does, check what type of object it is, and then change direction compered to what object it is its colliding whit!

Lets say it collides whit a tree, then you know what size it is and what not, and then you know how he should move around it!

But, the best way would be to make a grid for your world, so he have better cords to follow and so you can also know what grid is or is not occupied by something!

You probably want to look into steering behaviours. They are simple to implement, and you can do some really cool stuff with it:

That doesn’t sound like something you want to be doing all alone. Anyway, here’s basic logic:

In your update function, before you move towards the enemy transform, use Raycasting to check if there’s an obstacle in front of your transform. If no, good, if yes you are in big trouble. The almost painless way to move after that is to Raycast again to the left or the right, move a bit in one direction and try again. You would be better off using a package from the Asset Store, though. Good luck anyway! :slight_smile:

Or if there is a object in your way (found by a ray cast), use vector3 cross product to get the perpendicular vector, add it to your velocity, move and check again with a raycast afterwards. But then you almost already implemented obstacle avoidance. And learned some basic vector math which is important for game programming! Bonus hehe

oldcollins -

I am going to assume that you are trying to solve this issue as part of your game development, rather than as an exercise in coding.

Pathfinding and Steering are not trivial subjects. There are several opensource, free or paid solutions. There is also a solution built in to Unity, but this solution requires pro (NavMesh).

What you are looking for is Pathfinding, I feel, rather than Steering, tho’ these two fields do cross over.

There are several solutions on the AssetStore:

Aron Grandberg is the one I’ve followed the most and used a version of a few years back and it was effective and my monster chased my player just fine.

Simple Path has good feedback and the author seems very knowledgeable.

Path is also one of the first to be around the Unity Community, and AngryAnt now works for Unity

You can find more by searching “Pathfinding” in the asset store.

Path is free. At one point Aron Grandberg’s package was free for non-commercial use and $100 for a commercial game. I have not looked at the software recently. His home page is here: http://www.arongranberg.com Dig around and see if there is still a free option.

The basic principles of pathfinding are well documented. I would be searching for A* (a-star) and NavMesh. Essentially, the method here is to periodically scan the game board for all possible paths from one point to another point by dividing the board into a grid, and then picking the shortest path - then moving the agent along that path until it reaches its destination.

Steering is simply moving towards a target in 2D or 3D space. A complex steering solution will try to do things like reach a target, avoid a neighbor, etc. and all of these steering needs will be wrapped into one force or direction.

Pathfinding and Steering cross over when there are obstacles in the way. Often steering can fail in a situation where only steering is being used when trying to avoid obstacles. A Pathfinding solution can be useful in these cases to efficiently get around an obstacle. In the very least, steering often needs a state machine to help influence the style of steering used at any one time that changes depending upon the situation.

One resource site is http://aigamedev.com and might be worth a poke around. I would also look into A* and NavMesh.

You could try to “roll your own”, but some of the projects have been working for years, so in general, I would suggest looking to see if there is a ready-made or ready-to-modify solution that fits your budget, even if it’s free.

Little angel is right! If you just need to go around static objects then i misunderstood it and pathfinding algorithms of some sort are a better solution. However if you need to avoid other non static objects like other enemies then steering are really useful, but ofcause a mix of both is perfect.

If your in no hurry, then i can mention that i will hopefully put up a complete astar solution with both a grid based and waypoint based solution on the assetstore within a few weeks, and for free untill im sure the product is good enough to charge for (will never be more than 10 bucks anyways)

i’ve been looking into pathfinding and im also looking at raycasting so is there any chance someone can help me implement raycasting it to my script please

heres part of the up dated code with a basic raycast that prints “There is something in front of the object!”

function Update () {
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 
 //Raycast
var fwd = transform.TransformDirection (Vector3.forward);


   
     if (Physics.Raycast (transform.position, fwd, 10)) {
        print ("There is something in front of the object!");
    }
}

i’m trying to get the AI object to look for objects in a scene and collect them before the player does if it helps people know where i’m going with this scripting

here’s a new script update with working raycaster buts moving to fast any ideas on how to slow it down or improve the script please

var target : Transform; //the enemy's or item target
var target2 : Transform;
var target3 : Transform;

var moveSpeed = 3; //move speed

var rotationSpeed = 3; //speed of turning

var myTransform : Transform; //current transform data of this enemy



 


function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
     target = GameObject.FindWithTag("pickup").transform; //target the player

}

function Update () {
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

 
  //Raycast
   
// the directional vector of our target
var dir = (target.position - transform.position).normalized;
var hit : RaycastHit;
// Check for forward raycast
if(Physics.Raycast(transform.position, transform.forward, hit, 20)){
	if(hit.transform != transform){
		dir += hit.normal * 20;
	}
	
}

var leftr = transform.position;
var rightr = transform.position;

leftr.x  -= 4;
rightr.x += 4;

if(Physics.Raycast(leftr, transform.forward, hit, 20)){

	if(hit.transform != transform){
		dir += hit.normal * 20;
	}
	
}

if(Physics.Raycast(rightr, transform.forward, hit, 20)){

	if(hit.transform != transform){
		dir += hit.normal * 20;
	}
	
}


var rot = Quaternion.LookRotation(dir);

transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
transform.position += transform.forward * 20 * Time.deltaTime;

}

Are you saying this agent is moving too fast? Is this not controlled by “moveSpeed”? If so, you could reduce the value associated with “moveSpeed”.

I’m curious: Are you using this as an exercise to learn more about scripting? Or more about Unity? I’m still unclear why you are not working with known pathfinding libraries, which could so most of the heavy lifting you are struggling with.

If we’ve misunderstood, could you post a demo web-player to show the problem?

I’ve been playing around with pathfinding and i have found that there is a way to do what i’m looking for and now all I’ve got to do is find away for the AI to collect multiple targets whale moving on a random path generator.