• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ValintBence · Apr 06 at 12:01 PM · unity 5raycastcollidersrotate object

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Pineapple · Apr 06 at 02:36 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

284 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast without collider 1 Answer

the "blocking object" is not work 0 Answers

RaycastHit Collider Question 1 Answer

Unity raycast only sometimes works 0 Answers

What is the difference between Animator Controller and Animation component? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges