Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Dec 14, 2021 at 01:59 PM by Araxxin for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Araxxin · Dec 15, 2021 at 11:27 AM · raycast3dcollidersraycasting

Raycast not hitting the collider properly it always has a weird offset

Hey guys

So I have a weird problem and I hope someone can help me.

I have an interact script that sends out a raycast in the middle of the camera and checks if it collides to a specific tag I made. The problem now is with every object or item I make whether it's a box collider or a mesh collider it never accurately hits the object with the raycast and it just feels wonky, in game it's not a nice feeling, and I can't seem to find out what the problem with it is. I tried making the box colliders bigger and added a layermask on it but nothing seems to work. Any ideas what causes that wierd offset I have?

alt text alt text

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI;

 public class Interact : MonoBehaviour
 {
 
     Camera mainCam;
 
     GameObject clickedExaminableObject;
     MyDoorController clickedDoorObject;
     KeyItemController clickedUnlockableDoorObject;
     KeyItemController KeyItems;
     CollectibleController clickedCollectableObject;
     ClearTextController clearTextObject;
     LightController clickedLightObject;
     FlashlightController flashlight;
     LockController clickedLock;
     public LockController SafeLock;
     StatueController statue;
     SafeController safe;
     public MouseLook MouseLook;
     public PlayerMovement PlayerMovement;
 
     public GameObject SafeLockUI;
     public bool safeUI = false;
     
 
     string value;
     Vector3 originalPosition;
     Quaternion originalRotation;
     bool examineMode;
     public bool readingMode;
     bool buttonhasbeenpressed;
 
 
     public int rayLength = 5;
     public float distance = 1;
     public float rotationSpeed = 10;
     public Image crosshair = null;
     public bool isCrosshairActive;
     public bool doOnce;
 
     
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         mainCam = Camera.main;
         examineMode = false;
         readingMode = false;
 
     }
 
     // Update is called once per frame
     void Update()
     {
         clickedObject();
         TurnObject();
         ExitExamineMode();
 
         
 
     }
 
     public void buttonName()
     {
         
         if (SafeLock != null)
         {
             value = EventSystem.current.currentSelectedGameObject.name;
             print(value);
             print("clickedLock");
             SafeLock.SetValue(value);
         }
     }
 
     void clickedObject()
     {
         RaycastHit hit;
         Ray ray = mainCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
         int layerMask = 1 << 9;
         
 
         //Places the object with the tag clickable in to the examine state
         if (Physics.Raycast(ray, out hit, rayLength, layerMask) && hit.collider.tag == "Examinable" && examineMode == false)
         {
             if (!doOnce)
             {
                 clearTextObject = hit.collider.gameObject.GetComponent<ClearTextController>();
                 CrosshairChange(true);
             }
 
             isCrosshairActive = true;
             doOnce = true;
 
             if (Input.GetKeyDown("e"))
             {
                 print("clickedExaminableObject");
                 clickedExaminableObject = hit.transform.gameObject;
                 originalPosition = clickedExaminableObject.transform.position;
                 originalRotation = clickedExaminableObject.transform.rotation;
 
                 clickedExaminableObject.transform.position = Camera.main.transform.position + Camera.main.transform.forward * distance;
 
                 Time.timeScale = 0;
 
                 examineMode = true;
             }
 
         }
         else if (Input.GetKeyDown("e") && readingMode == false && examineMode == true)
         {
             print("is reading");
 
             clearTextObject.readCleartextEnable();
             readingMode = true;
         }
         else if (Input.GetKeyDown("e") && readingMode == true)
         {
             clearTextObject.readCleartextDisable();
             readingMode = false;
         }
 
         else if (Physics.Raycast(ray, out hit, rayLength, layerMask) && hit.collider.tag == "Openable")
         {
             if (!doOnce)
             {
                 clickedDoorObject = hit.collider.gameObject.GetComponent<MyDoorController>();
                 CrosshairChange(true);
             }
 
             isCrosshairActive = true;
             doOnce = true;
 
             if (Input.GetKeyDown("e"))
             {
                 print("clickedOpenableObject");
                 clickedDoorObject.PlayAnimation();
             }
 
         }
         //if the Raycasts hits an Object with the tag Unlockable it will try to open the door and respond accordingly to the other scripts
         else if (Physics.Raycast(ray, out hit, rayLength, layerMask) && hit.collider.tag == "Unlockable")
         {
             if (!doOnce)
             {
                 clickedUnlockableDoorObject = hit.collider.gameObject.GetComponent<KeyItemController>();
                 CrosshairChange(true);
             }
             isCrosshairActive = true;
             doOnce = true;
 
             if (Input.GetKeyDown("e"))
             {
                 print("clickedUnlockableObject");
                 clickedUnlockableDoorObject.DoorInteraction();
             }
 
         }
 
 
         else if (Physics.Raycast(ray, out hit, rayLength, layerMask) && hit.collider.tag == "Lightable")
         {
             if (!doOnce)
             {
                 clickedLightObject = hit.collider.gameObject.GetComponent<LightController>();
                 CrosshairChange(true);
             }
             isCrosshairActive = true;
             doOnce = true;
 
             if (Input.GetKeyDown("e"))
             {
                 print("clickedLightableObject");
                 clickedLightObject.turnLightOn();
             }
         }
         else if (Physics.Raycast(ray, out hit, rayLength, layerMask) && hit.collider.tag == "Lock")
         {
             print("lock");
 
             if (!doOnce)
             {
                 clickedLock = hit.collider.gameObject.GetComponentInParent<LockController>();
                 CrosshairChange(true);
             }
             isCrosshairActive = true;
             doOnce = true;
 
             if (Input.GetKeyDown("e"))
             {
                 if (clickedLock != null)
                 {
                     print("clickedLock");
                     string value = hit.collider.gameObject.transform.name;
                     print(value);
                     clickedLock.SetValue(value);
                 }
 
             }
         }
         else if (Physics.Raycast(ray, out hit, rayLength, layerMask) && hit.collider.tag == "Flashlight")
         {
             if (!doOnce)
             {
                 flashlight = hit.collider.gameObject.GetComponentInParent<FlashlightController>();
                 CrosshairChange(true);
             }
             isCrosshairActive = true;
             doOnce = true;
 
             if (Input.GetKeyDown("e"))
             {
                 print("pickedupFlashlight");
                 flashlight.PickUp();
             }
         }
         else if (Physics.Raycast(ray, out hit, rayLength, layerMask) && hit.collider.tag == "KeyItems")
         {
             if (!doOnce)
             {
                 KeyItems = hit.collider.gameObject.GetComponent<KeyItemController>();
                 CrosshairChange(true);
             }
             isCrosshairActive = true;
             doOnce = true;
 
             if (Input.GetKeyDown("e"))
             {
                 KeyItems.KeyItems();
             }
         }
         else if (Physics.Raycast(ray, out hit, rayLength, layerMask) && hit.collider.tag == "Statue")
         {
             if (!doOnce)
             {
                 statue = hit.collider.gameObject.GetComponent<StatueController>();
                 CrosshairChange(true);
             }
             isCrosshairActive = true;
             doOnce = true;
 
             if (Input.GetKeyDown("e"))
             {
                 statue.StatueInteraction();
             }
         }
         else if (Physics.Raycast(ray, out hit, rayLength, layerMask) && hit.collider.tag == "Safe")
         {
             if (!doOnce)
             {
                 safe = hit.collider.gameObject.GetComponent<SafeController>();
                 CrosshairChange(true);
             }
             isCrosshairActive = true;
             doOnce = true;
 
             if (Input.GetKeyDown("e") && safeUI == false && SafeLock.NumberCodeisRight == false)
             {
                 SafeLockUI.SetActive(true);
                 //SafeLock.GetComponent<LockController>();
                 Cursor.visible = true;
                 Cursor.lockState = CursorLockMode.Confined;
                 PlayerMovement.enabled = false;
                 MouseLook.enabled = false;
                 safeUI = true;
 
                 
 
 
             }
             else if (Input.GetKeyDown("e") && safeUI == true)
             {
                 SafeLockUI.SetActive(false);
                 Cursor.visible = false;
                 Cursor.lockState = CursorLockMode.Locked;
                 PlayerMovement.enabled = true;
                 MouseLook.enabled = true;
                 safeUI = false;
             }
         }
         else
         {
             if (isCrosshairActive)
             {
                 CrosshairChange(false);
                 doOnce = false;
             }
         }
 
     }
     
 
     void CrosshairChange(bool on)
     {
         if (on && !doOnce)
         {
 
             crosshair.enabled = true;
             crosshair.color = Color.red;
         }
 
         else
         {
             crosshair.enabled = false;
             crosshair.color = Color.white;
             isCrosshairActive = false;
         }
     }
     //ExaminableObject will be rotated by left mouseclick
     void TurnObject()
         {
             if (Input.GetMouseButton(0) && examineMode == true)
             {
                 print("is rotating");
 
 
                 float xAxis = Input.GetAxis("Mouse X") * rotationSpeed;
                 float yAxis = Input.GetAxis("Mouse Y") * rotationSpeed;
                 clickedExaminableObject.transform.Rotate(Vector3.up, -xAxis, Space.World);
                 clickedExaminableObject.transform.Rotate(Vector3.right, yAxis, Space.World);
             }
         }
 
 
         //Examinemode will be exited and position of the Object will be reseted by hitting the escape key
         void ExitExamineMode()
         {
             if (Input.GetKeyDown("escape") && examineMode == true)
             {
                 clickedExaminableObject.transform.position = originalPosition;
                 clickedExaminableObject.transform.rotation = originalRotation;
 
                 Time.timeScale = 1;
 
                 examineMode = false;
 
                 clearTextObject.readCleartextDisable();
                 readingMode = false;
             }
         }
 
 }
 


raycast-not-hitting-collider.png (225.1 kB)
raycast-hitting-collider-with-a-weird-offset.png (219.8 kB)
Comment
Add comment
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

168 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

Related Questions

Raycasting to make Buildings Clickable 1 Answer

How can I let the ray to the edge of the capsule? 1 Answer

Drawing a 3d Cone ray 2 Answers

RTS Object selection Ignoring Colliders 2 Answers

Raycast origin overlapping surface of collider counts as a hit 0 Answers


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