- Home /
Why is my ray-cast not recognising objects that are rotated?
I am creating a game where my character can pick up and drop items. However, after picking up the item and dropping it the character can not pick it up again unless the rotation of the object stays the same (the item stays facing upwards like it's instantiated). After researching I found out that rays don't always work if an object is rotated. Here is my script for the pickup system:
public class PickUpItem : MonoBehaviour
{
public ItemScript script;
public Rigidbody rb;
public MeshCollider coll;
public Transform player, fpCam, container;
public float pickUpRange;
public float dropForwardForce, dropUpwardForce;
private bool playerexists;
public GameObject item = null;
private Ray interactionRay;
public bool equipped;
public static bool slotFull;
private void Update()
{
if(InteractRayCast() != null && !slotFull) {
item = InteractRayCast();
setItem(item);
Debug.Log("Item found!");
}
if(InteractRayCast() == null && !slotFull) {
item = null;
script = null;
coll = null;
rb = null;
}
Vector3 distanceToPlayer = player.position - transform.position;
if(script != null) {
Debug.Log("Waiting for Key");
if(!equipped && distanceToPlayer.magnitude <= pickUpRange && Input.GetKeyDown(KeyCode.E) && !slotFull) pickUpItem();
if(equipped && Input.GetKeyDown(KeyCode.Q)) dropItem();
}
}
private void pickUpItem()
{
equipped = true;
slotFull = true;
item.transform.SetParent(container);
item.transform.localPosition = Vector3.zero;
item.transform.localRotation = Quaternion.Euler(Vector3.zero);
rb.isKinematic = true;
coll.isTrigger = true;
script.enabled = true;
Debug.Log("Item Picked Up");
}
private void dropItem()
{
equipped = false;
slotFull = false;
item.transform.SetParent(null);
rb.isKinematic = false;
coll.isTrigger = false;
rb.velocity = player.GetComponent<Rigidbody>().velocity;
rb.AddForce(fpCam.forward * dropForwardForce, ForceMode.Impulse);
rb.AddForce(fpCam.up * dropUpwardForce, ForceMode.Impulse);
float random = Random.Range(-1f, 1f);
rb.AddTorque(new Vector3(random, random, random) * 10);
script.enabled = false;
Debug.Log("Item Dropped");
script = null;
coll = null;
rb = null;
item = null;
}
void setItem(GameObject obj)
{
script = obj.GetComponent<ItemScript>();
rb = obj.GetComponent<Rigidbody>();
coll = obj.GetComponent<MeshCollider>();
}
GameObject InteractRayCast()
{
Vector3 playerPos = player.position;
Vector3 forwardDir = player.forward;
interactionRay = new Ray(playerPos, forwardDir.normalized);
RaycastHit interactionRayHit;
float interactionRayLength = pickUpRange;
Vector3 interactionRayEndpoint = forwardDir * interactionRayLength;
// Debug.DrawLine(playerPos, interactionRayEndpoint);
bool hitFound = Physics.Raycast(interactionRay, out interactionRayHit, interactionRayLength);
if(hitFound)
{
GameObject hitObject = interactionRayHit.transform.gameObject;
string hitFeedback = hitObject.name;
Debug.Log(hitFeedback);
return hitObject;
}
else
{
return null;
}
}
}
How can I check what game object my character is looking at if rays do not work with rotating objects?
In general raycasts do not care for the rotation of an object. To properly debug what is going on please draw your current raycasts in the sceneview by adding a Debug.DrawRay call to your code.
When you have this then reproduce the issue and take a screenshot of your sceneview where one can see the collider of the object and the raycast.
Also make sure the collider is not disabled somehow.
Answer by ValintBence · Apr 06 at 06:21 PM
I found out the object was around 5.4 units away with a thickness of 0.3 units, so the ray was not actually reaching the object and also the ray was attached to the player body and not the camera. Thank you for helping me out!
Your answer
Follow this Question
Related Questions
the "blocking object" is not work 0 Answers
RaycastHit Collider Question 1 Answer
Unity raycast only sometimes works 0 Answers
Raycast without collider 1 Answer
Unity 5 no terrain collision 1 Answer