Hi there.
I try to make a simple Basic AI, that approaches a Target, and turn around if its near enougth, but all it do is approach the Target, until it is in it, and start rotate.
Thats what i got until now.
public float spacetotarget;
private GameObject target;
//approach Target
if(Physics.Raycast(transform.position,target.transform.position,spacetotarget,10)){
Debug.Log("zunah");
targetrotation = Quaternion.Inverse(Quaternion.LookRotation(target.transform.position - transform.position));
}
else{
targetrotation = Quaternion.LookRotation(target.transform.position - transform.position);
}
The Debug.Log dont show up.
The Target has a Collider and the Layer 10.
You have several mistakes here:
Raycast uses a position from where to start and a direction vector. However you used a position as direction.
The layermask parameter is a bitmask and not a layer index. You effectively are casting against layer 1 (value 2) and layer 3 (value 8).
To fix this you could use a Linecast instead of a Raycast which takes two positions instead of a position and a direction.
When using a Raycast you should calculate the direction vector to your target by calculating
target.transform.position - transform.position
To get a layermask that only has layer 10 in it (start counting by 0) just use
2^10
or precalculate the value which equals:
1024
Here’s a woring example:
Vector3 dir = target.transform.position - transform.position;
int layermask = 2^10;
if(Physics.Raycast(transform.position, dir, spacetotarget, layermask))
Find a Way:
the declaration of the Layermask is a bit tricky.
int targetlayer = 10;
targetlayer = ~targetlayer;
Vector3 direction = target.transform.position-transform.position;
if(Physics.Raycast(transform.position,direction,spacetotarget,targetlayer)){
targetrotation = Quaternion.Inverse(Quaternion.LookRotation(target.transform.position - transform.position));
}
else{
targetrotation = Quaternion.LookRotation(target.transform.position - transform.position);
}
Griffo
July 14, 2013, 5:26pm
4
This is the way I go with layers …