# More realistic AI's

I have made simple AI's that follow you around i didnt use the handy look at function i used a rotation with dampening to make it more realistic but the problem is if there is a wall or my case a building and im on onside and its on the other itll just keep running into a wall how do i fix that. and also on an unrelated note how do i save a scene so that when i go to a new area and come back itll be the way i left. form example say i start in the middle of a room and leave through a door into a new scene when i come back i wanna be right by the door not in the middle of the room.how do i save the scene to do tht?

if there is a wall or my case a building and im on onside and its on the other itll just keep running into a wall how do i fix that

You need to have some sort of path finding or obstacle avoidance algorithm. A* is a popular algorithm that can solve problems regarding finding a path from one place to another. It is way out of the scope of unityAnswers to answer that, but you can find sturdy tutorials on A* all over the web.

and also on an unrelated note how do i save a scene so that when i go to a new area and come back itll be the way i left

Complicated. You basically have to keep track of every state in every object by yourself. I did this in a project using reflection. I think there are some third party alternatives out there too, if you google a bit.

If you are happy just remembering the position of your character, you could do with a static Dictionary that keeps track of the position of the last visit in any level. You'd index it with the level name, and the position would be the value. You probably want to change the Vector3 type to something more complex, such as a pair of Vector3 and Quaternion so you remember orientation as well.

Here is a suggestion starter... You'd have to consider how your existing project work in order to include it to your project.

``````using System.Collections.Generic;
using UnityEngine;

public struct LevelSave
{
public Vector3 Position { get; set; }
public Quaternion Rotation { get; set; }

public static LevelSave FromTransform ( Transform transform )
{
return new LevelSave( )
{
Position = transform.position,
Rotation = transform.rotation
};
}
}

public class LevelData : MonoBehaviour
{
private Dictionary<int, LevelSave> levelSaves = new Dictionary<int, LevelSave>( );

void Start ( )
{
}

public void Save ( )
{
levelSaves[ Application.loadedLevel ] = LevelSave.FromTransform( transform );
}

void OnLevelWasLoaded ( int level )
{
LevelSave save;
if ( levelSaves.TryGetValue( level, out save ) )
{
// Require receiver for easier debugging.
SendMessage( "OnLevelData", save, SendMessageOptions.RequireReceiver );
}
}
}

``````

It will call void OnLevelData(LevelSave levelSave) on the same game object, allowing an object to restore its position and rotation from the LevelSave structure. In order to save, you can just Invoke("Save");

It is a crude version but it should give the idea how you could establish some sort of persistancy. Note that the script sets the game object it is attached to as not to destroy on load, so any other scripts that are on this object also will live longer than one level.