Hi everyone.
I have a very strange probleme that I don’t understand. I apologize for my english if there’s many mistakes.
To get to the point as quick as possible, here is the situation : (C# in unity, using Visual Studio)
I’m making a 2D fighting game with unity (first game, i’m learning code), and I want to check if a player can see if his opponent has arrived or not yet. To do so, I use 2 raycasts (that starts just outside de player’s collider), one go to the left and the other to the right. There is no layers exeptions of any kind in my raycast code (like you can see) and the way I made the raycast, the only collider it can hit is the other player, if he’s there. But the thing is that the raycast hit nothing, even when the other player is here (there both raycasting and none of them find his opponent).
When I try to make the raycast go to the ground, it finds it with no problem, but the other player seems to be invisible for him. Even when he has the same layer of the ground !
The differences between the ground and the players are :
-The players are instantiated by a script after the start of the game (you have to press Space one time for each player with 2 players max)
-The players are not rendered like the ground (wich is a cube, in 3D I think), there rendered with animated sprites. But they have a collider, the same kind of the ground.
here is the script (attached to both of the players) with the raycast:
using UnityEngine;
using System.Collections;
public class playerOrientation : MonoBehaviour {
public GameObject player;
public GameObject enemy;
private Vector2 raycastOriginR;
private Vector2 raycastOriginL;
private RaycastHit2D hit;
private int enemyMask;
private int enemyLayer;
// Use this for initialization
void Start () {
player = this.transform.gameObject;
enemyLayer = 11;
enemyMask = 1 << enemyLayer;
}
// Update is called once per frame
void Update () {
// these vector2 set the origin of the raycast (outside the player's collider)
raycastOriginR = new Vector2(player.transform.position.x + 0.6f, player.transform.position.y + 0.2f);
raycastOriginL = new Vector2(player.transform.position.x - 1.5f, player.transform.position.y + 0.2f);
if(enemy == null){
hit = Physics2D.Raycast(raycastOriginR, Vector2.right, Mathf.Infinity);
Debug.DrawRay (raycastOriginR,Vector2.right * 10, Color.green); // my debug ray, works fine
Debug.Log("Raycast to RIGHT from " + player.name);
if(hit){
enemy = hit.transform.gameObject;
Debug.Log(player.name + " enemy found : " + enemy.name);
}else if(!hit){
hit = Physics2D.Raycast(raycastOriginL, Vector2.left, Mathf.Infinity);
Debug.DrawRay (raycastOriginL,Vector2.left * 10, Color.red);
Debug.Log("Raycast to LEFT from " + player.name);
if(hit){
enemy = hit.transform.gameObject;
Debug.Log(player.name + " enemy found : " + enemy.name);
}
}
}
}
}
I don’t know what I can tell you more about it. Just the whole code works perfectly well, and the console print this every frame :
Raycast to RIGHT from playerOne
Raycast to LEFT from playerOne
Raycast to RIGHT from playerTwo
Raycast to LEFT from playerTwo
etc…
I can see the rays going through the other players colliders but nothing happen. I search a long time but I don’t know what’s going on…
If you have any idea, please tell me.
Thank you very much for reading all of this