I plan to use a raycast for interacting with all things in my games but Im afraid itll be too laggy how can I shorten this code up?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StoreClerkOpen : MonoBehaviour
{
// Start is called before the first frame update
public GameObject StoreClerk;
public GameObject Store;
public GameObject Player;
public GameObject SCText;
// Whatever is your max distance (remove if not needed). However, it is nice to
have a max distance o which your monster can see the player.
float maxDistance = 10;
public bool LookingStoreClerk;
public bool StoreClerkOPEN;
void FixedUpdate()
{
// Will contain the information of which object the raycast hit
RaycastHit hit;
// if raycast hits, it checks if it hit an object with the tag Player
if(Physics.Raycast(transform.position, transform.forward, out hit,
maxDistance) &&
hit.collider.gameObject.CompareTag("StoreClerk"))
{
SCText.SetActive(true);
LookingStoreClerk = true;
}
if (Input.GetMouseButtonDown(0) && LookingStoreClerk)
{
StoreClerkOPEN = true;
SCText.SetActive(false);
Store.SetActive(true);
Player.GetComponent<SC_FPSController>().enabled = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
if (StoreClerkOPEN)
{
SCText.SetActive(false);
}
}
}
A short and easy code to detect if the player is looking at the store clerk and if they are a text pops up and they can click to interact. Problem is the code got slightly complicated and the text and raycast tend to glitch.
MonoBehaviour
andGameObject
. \$\endgroup\$