- Home /
The question is answered, right answer was accepted
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?
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;
}
}
}
Follow this Question
Related Questions
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
Voxel Raycasting Help 0 Answers
Boxcast vs Raycast Oddities 1 Answer