hi, im currently learning game development for a week, so i know nothing. i’m trying to develop interaction system in 3d thidrperson mode. here is my raycast script that i set for aiming and interaction censor attached to main camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class raycastaimpoint : MonoBehaviour
{
public Transform maincamera;
public Transform player;
private int rayLength = 500;
public LayerMask interactable;
public Image crosshair;
public bool detected;
void start()
{
detected = false;
}
void crosshairActive()
{
crosshair.color = Color.red;
detected = true;
}
void crosshairNormal()
{
crosshair.color = Color.white;
detected = false;
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, out hit))
{
Vector3 hitPoint = hit.point;
player.LookAt(hitPoint);
}
else
{
Vector3 farthestPoint = transform.position + fwd * rayLength;
player.LookAt(farthestPoint);
}
Debug.DrawLine(transform.position, hit.point, Color.yellow, 0.5f * Time.deltaTime);
transform.rotation = maincamera.rotation;
if(Physics.Raycast(transform.position, fwd, 20f, interactable))
{
crosshairActive();
}
else
{
crosshairNormal();
}
}
}
crosshair works well when aim to interactable object turn red, and can turn white again.
problem is i want to create other script for managing interaction based on crosshair color change. if it turn red i want to hit “e” key and some thing happen. ive tried some answer here but no success… tying something like this script attached to interactable game object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class interactaableScript : MonoBehaviour
{
public raycastaimpoint censor;
void update()
{
if(censor.detected)
{
Debug.Log("something");
}
}
}
thanks for your time guys, peace!