- Home /
My Raycast dont hit.
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.
Answer by Projekt-Krieg · Jul 14, 2013 at 11:19 AM
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);
}
Your layermask makes no sense. You just inverted the mask. So ins$$anonymous$$d of layer 1 and 3 you include all except 1 and 3.
Your value of 10 equals this bitmask:
00000000 00000000 00000000 00001010
The inverted mask would look like this:
11111111 11111111 11111111 11110101
If you want to only cast agains layer 10 the mask should look like this:
00000000 00000000 00000100 00000000
|
\___________Layer 10 (the 11th layer)
This equals the value of 1024 or 2^10.
I guess you just don't know what you actually want...
private int targetlayer = 1<<10;
int bittargetlayer = ~targetlayer;
if(Physics.Linecast(transform.position,target.transform.position,bittargetlayer));
this is my final solution.
Answer by Bunny83 · Jul 14, 2013 at 10:35 AM
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))
$$anonymous$$ake sure your "spacetotarget" is large enough to actually reach the target.
Changed my code to:
Vector3 direction = target.transform.position-transform.position;
Layer$$anonymous$$ask justhit = 2^10;
if(Physics.Raycast(transform.position,direction,spacetotarget,justhit)){
Debug.Log("zunah");
targetrotation = Quaternion.Inverse(Quaternion.LookRotation(target.transform.position - transform.position));
}
else{
targetrotation = Quaternion.LookRotation(target.transform.position - transform.position);
}
but it still approaches and rotate in the target.
spacetotarget is 500 so it should work.
Your answer
Follow this Question
Related Questions
Why Do Raycasts Ignore NavMesh Agents 4 Answers
Collider Vision AI question. Solved! 0 Answers
Character Controller can pass through Collider 1 Answer
Using a raycast without colliders 0 Answers
About a enemy AI in my game 1 Answer