C
C#2y ago
Kaelen

❔ My Raycast seems to be upside down

Hello, the debug log return my prefab, however this prefab is the start point of my raycast ? How is it possible and how to resolve it please ?
15 Replies
Anton
Anton2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Anton
Anton2y ago
the raycast hits the collider of your object probably
Kaelen
KaelenOP2y ago
How could I fix that? And how is it possible since that the object is the entry point ?
private RaycastHit2D hitInfo;

private GameObject grabbedObject;
private int layerIndex;

void Awake() {
layerIndex = LayerMask.NameToLayer("Objects");
hitInfo = Physics2D.Raycast(transform.position, -transform.right, 2f, layerIndex);
}
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

if (hitInfo.collider)
{
Debug.DrawRay(transform.position, -transform.right * 2f, Color.green);
Debug.Log(hitInfo.collider.name);
// //Prendre l'item
// if (Keyboard.current.spaceKey.wasPressedThisFrame && grabbedObject == null)
// {
// grabbedObject = hitInfoFridge.collider.gameObject;
// grabbedObject.GetComponent<Rigidbody2D>().isKinematic = true;
// grabbedObject.transform.position = grabPoint.position;
// grabbedObject.transform.SetParent(transform);
// }

// //Rel�cher l'objet
// else if (Keyboard.current.spaceKey.wasPressedThisFrame)
// {
// grabbedObject.GetComponent<Rigidbody2D>().isKinematic = false;
// grabbedObject.transform.SetParent(null);
// // grabbedObject = null;
// // }

} else {
Debug.DrawRay(transform.position, transform.right * 2f, Color.red);
}
}
}
private RaycastHit2D hitInfo;

private GameObject grabbedObject;
private int layerIndex;

void Awake() {
layerIndex = LayerMask.NameToLayer("Objects");
hitInfo = Physics2D.Raycast(transform.position, -transform.right, 2f, layerIndex);
}
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

if (hitInfo.collider)
{
Debug.DrawRay(transform.position, -transform.right * 2f, Color.green);
Debug.Log(hitInfo.collider.name);
// //Prendre l'item
// if (Keyboard.current.spaceKey.wasPressedThisFrame && grabbedObject == null)
// {
// grabbedObject = hitInfoFridge.collider.gameObject;
// grabbedObject.GetComponent<Rigidbody2D>().isKinematic = true;
// grabbedObject.transform.position = grabPoint.position;
// grabbedObject.transform.SetParent(transform);
// }

// //Rel�cher l'objet
// else if (Keyboard.current.spaceKey.wasPressedThisFrame)
// {
// grabbedObject.GetComponent<Rigidbody2D>().isKinematic = false;
// grabbedObject.transform.SetParent(null);
// // grabbedObject = null;
// // }

} else {
Debug.DrawRay(transform.position, transform.right * 2f, Color.red);
}
}
}
Anton
Anton2y ago
well if you shoot it from within the collider logically it would hit the collider colliders are volumes rather than surfaces
Kaelen
KaelenOP2y ago
but I precised that only some layers should be triggered
Anton
Anton2y ago
ah i see your error the raycast takes a layer mask not the layer index do a bitshift 1 << layerIndex I don't remember actually, you may need to invert it too ~(1 << layerIndex)
Kaelen
KaelenOP2y ago
this one doesnt change anything this one set the drawray to red, but when I hit the layer that I want to target, it stay to red
Anton
Anton2y ago
"it stay to red"? that's not english, can you word it differently?
Kaelen
KaelenOP2y ago
sorry Im french the color of the layer is red even when I hit the layer in question
Anton
Anton2y ago
so?
Kaelen
KaelenOP2y ago
so the hit.collider always stays to null
Anton
Anton2y ago
there's no code that would change it you probably meant to do the raycast in update awake only runs once
Kaelen
KaelenOP2y ago
private RaycastHit2D hitInfo;

private GameObject grabbedObject;
private int layerIndex;

void Awake() {
layerMask = LayerMask.GetMask("Objects");
hitInfo = Physics2D.Raycast(transform.position, -transform.right, 2f, layerMask);
}
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

if (hitInfo.collider) // never set to true
{
Debug.DrawRay(transform.position, -transform.right * 2f, Color.green);
Debug.Log(hitInfo.collider.name); // renvoie mon prefab, or c'est le poin d'entrée


} else {
Debug.DrawRay(transform.position, transform.right * 2f, Color.red);
}
}
}
private RaycastHit2D hitInfo;

private GameObject grabbedObject;
private int layerIndex;

void Awake() {
layerMask = LayerMask.GetMask("Objects");
hitInfo = Physics2D.Raycast(transform.position, -transform.right, 2f, layerMask);
}
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

if (hitInfo.collider) // never set to true
{
Debug.DrawRay(transform.position, -transform.right * 2f, Color.green);
Debug.Log(hitInfo.collider.name); // renvoie mon prefab, or c'est le poin d'entrée


} else {
Debug.DrawRay(transform.position, transform.right * 2f, Color.red);
}
}
}
its a old code I change the layer index to the layer mask
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server