Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Serreas · Feb 25, 2016 at 02:10 PM · raycasthit

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);
             }
 
 
         }
     }
 }
 

  
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
             }
         }
     }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Serreas · Feb 26, 2016 at 12:48 AM 0
Share

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);
              }
          }
avatar image
1

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)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Serreas · Feb 26, 2016 at 12:52 AM 0
Share

Also thank you for the help and the extra bit of information to keep in $$anonymous$$d. Your way works just as well!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

35 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RayCastHit Not Returning Rigidbody 0 Answers

raycast trigger specific animations 1 Answer

Issues with detecting collision with objects that have certain tags using Raycast 0 Answers

Physics Raycast not working 1 Answer

Physics.Raycast Not Working... Kinda 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges