- Home /
2d Raycast problem from enemy to player C#
The ray seems to detect the player but only when the player is standing "in" the enemy (they arent colliding).. i want the ray to trace to the player and to stop at the platform the player is standing on so that the player isn't "seen". The ray is going from the enemy to the "target" aka the player. All of this is in FixedUpdate. Thanks in advance / C# newbie
Debug.DrawRay(myTransform.position, target.position - myTransform.position, Color.red); //debug ray to see the ray
RaycastHit2D hit = Physics2D.Raycast(myTransform.position, target.position - myTransform.position);//from my transform in the direction to the player as seen in the debug ray
if (hit.collider.tag == "Player")//if the object that was hit goes by the name "Player" then:
{
Debug.Log("HIT");//write HIT
}
if (hit.collider.tag == "SightBreak")//if the object that was hit goes by the name "SightBreak" then:
{
Debug.Log("cant see!!!!!!!!!!!");//write cant see!!!!!!!!!!!!!
}
i have also put the platform in the layer "SightBreak" just in case.
Also worth noting may be that the platform collider turns of when the player is standing under it(so that the player may jump on to the platform from under.). Shouldn't be a problem though since the player i standing on the platform and therefore the platform collider is on.
Can you add some information what's the current behaviour with this script? Does it print anything at all? Do all objects have a 2d collider?
Try debugging to see what the ray hit ins$$anonymous$$d of what the ray should hit.
ok so i changed the debug to write what it collides with as you said and it now keeps printing "Enemy" until the player stands "in" the enemy where it then prints out a few "Player" over meanwhile "Enemy"is being printed. The goal is to make the enemy walk towards the player when the player is seen but that's not important right now. Right now i only need the ray to work correctly. Here's the whole code of the enemy.
private Transform myTransform;//my (the enemy's)transform
public Transform target;//The player't transform
public int maxDistance;//detection distance
public int moveSpeed = 3;//movement speed
public int Direction;//direction of movement and sprite
void Awake(){
myTransform = transform;//init my transform
}
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");//init player
target = go.transform;//init player transform
maxDistance = 10;//init detectiondistance
}
void Update () {
if (target.position.x > myTransform.position.x +1) {
Direction = 1;
} else if(target.position.x < myTransform.position.x -1){
Direction = -1;
}
if(Vector2.Distance(target.position, myTransform.position) < maxDistance){
//$$anonymous$$ove towards target
myTransform.position += myTransform.right * moveSpeed * Direction * Time.deltaTime;
}
gameObject.transform.localScale = new Vector2 (Direction, 1);
}
void FixedUpdate(){
Debug.DrawRay(myTransform.position, target.position - myTransform.position, Color.red); //debug ray to see the ray
RaycastHit2D hit = Physics2D.Raycast(myTransform.position, target.position - myTransform.position);//from my transform in the direction to the player as seen in the debug ray
if (hit.collider != null)//if the object that was hit goes by the name "Player" then:
{
Debug.Log(hit.collider.tag);//write HIT
}
/*if (hit.collider.tag == "Player")//if the object that was hit goes by the name "Player" then:
{
Debug.Log("HIT");//write HIT
}
if (hit.collider.tag == "SightBreak")//if the object that was hit goes by the name "SightBreak" then:
{
Debug.Log("cant see!!!!!!!!!!!");//write HIT
}*/
}
Answer by legetic · Sep 07, 2015 at 03:49 PM
Thanks for the comments but i managed to solve it today, rest seems to have cleared my mind up hehe... I managed to figure out that the problem was that the ray collided with it self so i solved it with layermasking. This also meant i had to define a length of the ray but i just made it the distance between the enemy and player. My FixedUpdate:
int layerMask = 1 << 9;
layerMask = ~layerMask;
Debug.DrawRay(myTransform.position, target.position - myTransform.position, Color.red); //debug ray to see the ray
RaycastHit2D hit = Physics2D.Raycast(myTransform.position, target.position - myTransform.position,Vector2.Distance(target.position, myTransform.position),layerMask);//from my transform in the direction to the player as seen in the debug ray
if (hit.collider != null)//if the object that was hit goes by the name "Player" then:
{
Debug.Log(hit.collider.tag);//write HIT
}
Do i delete this question now since this might not help anyone?
Hi!
Another way to solve this would be to use a Linecast ins$$anonymous$$d of a Raycast. I have a short (less than 5 $$anonymous$$s) tutorial on how to use a Linecast and a re$$anonymous$$der to make sure the linecast's starting position is outside the collider :D There is also a full transcript with screen shots on my blog :) One of the really powerful things about program$$anonymous$$g is are always a million different ways to do something ^_^
Tutorial video: https://youtu.be/e9TX6DEPfUQ Blog: http://lifeofbuckets.blogspot.com/2015/08/linecastsandColliders.html
Your answer
Follow this Question
Related Questions
Raycast for a 2D objects 0 Answers
Raycasting in 2D is not working 0 Answers
2d raycast enemy detection problem. 0 Answers
Layer Mask on raycast2d not working? 1 Answer