Storing successive raycast hits in array?
Hi,
I have made this simple script which shoots out a raycast at the mouse position to the ground. Now I'm trying to figure out how I can store successive raycast hit positions (say for example you click the ground 3 times and store each position in a separate variable or array) in an array or seperate variables in order to use these vector3 position in later calculations. I have looked around the web trying to figure it out but couldn't really get a definite answer. It probably has something to do with an raycast array but I'm not sure how to implement that. Maybe somebody could help me with that. Here is the code.
using UnityEngine;
using System.Collections;
public class RayCasts : MonoBehaviour {
private Vector3 Point;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit,Mathf.Infinity))
{
Point = hit.point;
string s = Point.ToString ();
Debug.Log (string.Format ("Points {0}", s));
Debug.DrawRay (hit.point, hit.normal, Color.red,5f);
}
}
}
}
Answer by matias_ · Feb 25, 2016 at 03:43 PM
You can use a generic list to save all the hit points
using UnityEngine;
using System.Collections;
using System.Collections.Generic;//Lists are in System.Collections.Generic
public class RayCasts : MonoBehaviour {
private Vector3 Point;
public List<Vector3> PointList;//Create the variable, I made it public to watch the points in the Inspector
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit,Mathf.Infinity))
{
Point = hit.point;
PointList.Add(hit.point);//Adding the point to array
Debug.DrawRay (hit.point, hit.normal, Color.red,5f);
}
}
}
}
That works beautifully thanks so much! I've also added one more line to retrieve a vector3 value and display it with the debug comment for testing.
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit,$$anonymous$$athf.Infinity))
{
Point = hit.point;
PointList.Add(hit.point);//Adding the point to array
Debug.Log(PointList[0]); //enter index value of list to retrieve specific value
Debug.DrawRay (hit.point, hit.normal, Color.red,5f);
}
}
Answer by phil_me_up · Feb 25, 2016 at 02:50 PM
What you probably want to do is store your hit points as an array. You can either make a member variable:
Private Point[] points;
or
Private List<Point> points = new List<Point>();
Now, when you get a new point (within your raycast test), just add that point to the array. If you're using the first declaration above, then you might use something like:
points[0] = hit.point;
or the second method might be
points.Add(hit.point).
In both cases you can look up the value in the array with something like
Point testPoint = points[1];
If you're new to this, I'd recommend using the second method as it's more flexible. Just remember that whatever you do you'll need to clear the array when you want new data, and you'll want to make sure that you never go out of bounds (i.e. check the length / count of the array / list before accessing its contents)
Also thank you for the help and the extra bit of information to keep in $$anonymous$$d. Your way works just as well!