I need to change the state (enum) of an object with the tag “Activatable” on a raycast.
First of all I need a way to detect that object has that tag, so I know I can use it, then I need to change it’s state to Active (in another script).
I can do most of the second part once I have the object reference, but I have never used raycasts before and after Googling a bit, I haven’t found much on getting tagged object references from raycasts.
So far I have as shown below.
The issue at the moment is that the raycast doesn’t seem to exist, there is no drawn line, and no print.
using UnityEngine;
using System.Collections;
public class CameraRaycast : MonoBehaviour
{
private RaycastHit hit;
public bool raycasting = true;
public enum State
{
Down,
Pickup,
}
public State state;
public enum RayState
{
Miss,
Hit,
}
public RayState Raystate;
void Update()
{
if (state == State.Down)
{
}
else
{
}
if (Raystate == RayState.Hit)
{
}
else
{
}
if (raycasting)
{
if (Physics.Raycast(transform.localPosition, transform.TransformDirection(Vector3.forward), Mathf.Infinity))
{
print("Something Here");
Debug.DrawLine(transform.localPosition, transform.TransformDirection(Vector3.forward));
}
}
}
}
I’m not sure how it’s supposed to work correctly for your needs.
The script itself works fine, but apparently not the way you want to.
You can check that if you put the DrawLine outside of the inner if.
Try Transform.TransformPoint, with that you’ll always cast in the direction of your GOs forward axis.
You need to change the DrawLine argument then as well and expand its length a bit.
I actually have to correct something from the previous post. I thought the second parameter of the Raycast was a position and not a direction just like in the DrawRay method. It’s been a while.
So keep using TransformDirection for the raycast but use TransformPoint for drawing the ray as it expects a target position and not a direction.
And what do you mean by ‘it does not move’? When i used your script as it is in the first post, the rays target position was fixed (as long as no rotation occured) and that was because you use the direction for drawing, which will then be interpreted as a positional information.
I’d guess that the object which this script is attached to has a parent object?
If that is the case, transform.localPosition won’t be equal to transform.position, use latter one instead and try what’s happening. Otherwise you’d take the child’s position relative to the parent’s position as a starting point in world space which is not desired.
I had to stick the line setting raycastEmit in the Update function to get it to update the position of the camera, but it is working now.
The main issue as is, is that once the raycast has hit its target (tagged “Activatable”), the raycast continues beyond that object.
The main objective of this long-term is to have an object that can only set it’s target as Inactive when it is placed down, and that object’s target’s state gets set to Inactive.
Once this happens, the raycast object does not continue to set anying beyond that target until it is picked up again.
If you have ever played The Talos Principle, the idea initially came from the tripod devices used in the puzzles, mine is a bit different but this specific feature is near enough the same.