I’m working on an AI class and I almost have it finished. I keep getting errors on this script
private Transform[] target;
private float Distance;
private float lookAtDistance = 25.0f;
private float chaseRange = 15.0f;
private float attackRange = 1.5f;
private float moveSpeed = 5.0f;
private float Damping = 6.0f;
private float attackRepeatTime = 1;
void lookAt()
{
float rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
Severity Code Description Project File Line Suppression State
Error CS1061 ‘Transform’ does not contain a definition for ‘position’ and no extension method ‘position’ accepting a first argument of type ‘Transform’ could be found (are you missing a using directive or an assembly reference?) Beatdown C:\Users\garre\OneDrive\Documents\Projects\Games\Beatdown\Assets\BoxerAI.cs 92 Active
Error CS1503 Argument 2: cannot convert from ‘float’ to ‘UnityEngine.Quaternion’ Beatdown C:\Users\garre\OneDrive\Documents\Projects\Games\Beatdown\Assets\BoxerAI.cs 93 Active
How can I fix this?
Post the whole script, it seems you’re missing assembly references.
Error 1
Hmm ok. Error 1, You've created an array of transforms, `private Transform[] target` and than you access the array incorrectly by doing, `target.position`. An array is a holder, so you can't just get the position out of the array. You need to tell the array, which part of the array you are looking at by doing target[0] or target[1] etc.
E.G. You have 2 transforms in the scene. One is called Player 1, the other Player 2. Under the Transform target, you have both player 1 and player 2… So when you say target.position, how does unity know you mean player 1, or even player 2? So to fix this you type target[0].position. That [0] will reference the first Player in the transform, in this case player 1. if you did [1] it would reference the seconds transform in the array, player 2 in this case.
You could also loop through each transform in the array and edit them like so.
Transform[] players = new Transform[2];
for (int i = 0; i < players.Length; i++)
{
players*.position = new Vector3(0, 0, 0);*
}
Error 2
Im looking at this part of the error now.
> C:\Users\garre\OneDrive\Documents\Projects\Games\Beatdown\Assets\BoxerAI.cs 92 Active Error CS1503 Argument 2: cannot convert from ‘float’ to 'UnityEngine.Quaternion
Change float rotation = Quaternion.LookRotation(transform.position - transform.position);
to Quaternion rotation = Quaternion.LookRotation(transform.position - transform.position);
and that will solve your error message.
Extra info
Just as some extra information. i believe you are making an AI that can navigate to the player. In my opinion, working with rotations and Quaternions can be pretty tough. Try look at Unity’s Navigation system. [Navmesh][1]. It handles movement for you and can go around objects, if they are in the way. It’s really simple to set up.
Hope this helps and Good Luck ~ Jared
[1]: Unity3D and Nav Meshes in Five Minutes (Unity 5 NavMesh) - YouTube