Raycast only occurs on start
Hello, so I have a little problem with raycasting. I want to put a certain object on raycast hit location, but it appears to work only once on startup and then stop. Even if I move the starting location of raycast or rotate it, the object that should move to raycast hit, won't move.
Code:
using UnityEngine;
public class Crosshair : MonoBehaviour {
public GameObject crosshair;
private Vector3 hitLoc;
void Update ()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.forward, out hit))
{
hitLoc = hit.transform.position;
}
crosshair.transform.position = hitLoc;
}
}
Thank you for your time.
Answer by Aljazlego · Sep 05, 2017 at 10:13 AM
@JavaMcGee thank you for your answer.
This is an example picture. I found out raycast does indeed goes just forward in world space so I made a little calculation:
{
Vector3 objectForward = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, objectForward, Color.green);
RaycastHit hit;
if (Physics.Raycast(transform.position, objectForward, out hit))
{
hitLoc = hit.transform.position;
}
crosshair.transform.position = hitLoc;
}
In debug, everything is okey, raycast is indeed going from end of the gun forward. But now, my "aim" object only spawns on the end at every 90 degrees, it's not really following raycast. Or to be precise, I believe it spawns in the middle of the wall, game object. There are 4 walls limiting square play area and this object or "aim" only spawns at the centre of each, depending which is closer to raycast. So I'm wondering how could I do something like you can see on the picture? Turret rotates for 360 degrees and gun goes up and down. I'd always like to have an object or "aim" at the collision point between raycast and wall.
Thank you for your time.
Solution: Well I've found a solution, that fixed my problem:
{
Vector3 objectForward = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, objectForward, Color.green);
Ray ray = new Ray(transform.position, objectForward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "Enviroment")
{
hitLoc = ray.GetPoint(hit.distance);
}
}
crosshair.transform.position = hitLoc;
}
I calculated the distance between raycast start and end. I used this distance at ray.GetPoint, which does a ray that's as long as the distance between raycast start and end, which then spawns "aim" at that rays end location.
Again, thank you for your time.
Answer by JavaMcGee · Sep 05, 2017 at 03:39 AM
Not sure what you need, but do you need to rotate the vector forward by the transform rotation? Otherwise the ray always points only in the forard direction in world coords
Your answer
Follow this Question
Related Questions
Problem. Pick up and Grab object script, except all objects in scene are picked up instead of one. 0 Answers
Ray cast to object to transform, instead all objects with script transform. 0 Answers
OnMouseDrag doesn't wokr. 0 Answers
Make projectile move in certain direction depending on position for base object (C#) 1 Answer