- Home /
how to set up interaction method using raycast?
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!
Answer by N-8-D-e-v · Aug 09, 2020 at 06:13 PM
A couple things, first off, it is best practice to use the Pascal naming convention when naming Methods and Classes, like this
public class InteractableScript : Monobehaviour
not like this
public class interactablescript : Monobehaviour
And variables with Camel notation like this
public int thisIsATestInt;
(Which you actually already did)
now, your program will still function if you don't follow this convention, with the exception of your update method in your second script, which is why I bring up Pascal notation. You spell
void Update()
like
void update()
so that code will not do anything, so first change that (also you spelled interactable like interaactable). Next, you are on the right track with the rest of your code. You are sensing if something is detected by your raycast, so we can extend that to also sense if you are pressing down the 'e' key like so (I also fixed your naming conventions so it will look a little different)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InteractableScript : MonoBehaviour
{
public raycastAimPoint censor;
void Update()
{
if (censor.detected && Input.GetKeyDown(KeyCode.E) //will sense if something is detected and if we are pressing the E key
{
Debug.Log("do something");
}
}
}
If you have any other questions then don't hesitate to ask, hope this helps!
also need one more ")" after (KeyCode.E)..
SOLVED! see? that poor elemantary mistake ive made. problem solved just by changed a single character. thanks dude! ur my savior.
any way, is there any exeption for notation to affect things? like the one i did? just incase ive made same mistake in the future. or can u point me to some article that can explain that exeption. peace! @N8-
sorry for the late response, the na$$anonymous$$g convention will not affect the way your code runs, with the exception of monobehaviour methods https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.html
void Start()
{
}
void Update()
{
}
void Awake()
{
}
void OnEnable()
{
}
void OnCollisionEnter(Collision col)
{
}
also, consider using visual studio with Unity, which will catch errors for you and (somewhat) clearly show you what you did wrong, also make sure to accept my answer as correct
Your answer
![](https://koobas.hobune.stream/wayback/20220613005247im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
damage function not calling 0 Answers
Multiple Cars not working 1 Answer
Raycast2D is inconsistent 0 Answers
2 raycasts on the fpwalker/camera 1 Answer