Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Plane.Raycast Example.

Discussion in 'Scripting' started by ricardo.russon, Dec 12, 2008.

  1. ricardo.russon

    ricardo.russon

    Joined:
    Dec 8, 2008
    Posts:
    9
  2. Cu3e

    Cu3e

    Joined:
    Oct 27, 2008
    Posts:
    63
    I realize this answer is quite late. I was looking for the same thing as the original poster and had to do some studying... If there are other people searching for this answer sometime, here is a small example I hope will help :)

    Code (csharp):
    1.  
    2. var plane : Plane = new Plane(Vector3.up, Vector3.zero);;
    3.  
    4. function LateUpdate()
    5. {
    6.     if (Input.GetMouseButton(0))
    7.     {
    8.         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    9.  
    10.         var ent : float = 100.0;
    11.         if (plane.Raycast(ray, ent))
    12.         {
    13.             Debug.Log("Plane Raycast hit at distance: " + ent);
    14.             var hitPoint = ray.GetPoint(ent);
    15.            
    16.             var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
    17.             go.transform.position = hitPoint;
    18.             Debug.DrawRay (ray.origin, ray.direction * ent, Color.green);
    19.         }
    20.         else
    21.             Debug.DrawRay (ray.origin, ray.direction * 10, Color.red);
    22.  
    23.     }
    24. }
    25.  
     
    theolagendijk likes this.
  3. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,786
    better late than never!
     
  4. Exagerate

    Exagerate

    Joined:
    Feb 16, 2013
    Posts:
    13
    Definitely- I found this really useful, thanks so much. Use: isometric strategy game, building placement.
     
  5. jasmyn

    jasmyn

    Joined:
    Sep 27, 2012
    Posts:
    1
    Works great, thanks...

    One thing, there's no need to define ent, just declare it. Raycast() will set it for you and that 100.0 is just getting overwritten...

    Cheers
     
    Last edited: Dec 31, 2013
  6. superfryme

    superfryme

    Joined:
    Jun 9, 2017
    Posts:
    26
    Thanks a Million. Finally figured this out with my camera being rotated. here is my solution

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Tilemaps;
    6. public class MouseCursor : MonoBehaviour
    7. {
    8.     Plane plane;
    9.     public LoadGrid loadGrid;
    10.     Vector3Int ClickedTile;
    11.     public Tiledata tiledata;
    12.     private void Start()
    13.     {
    14.         plane = new Plane(loadGrid.transform.up, Vector3.zero);//camera is on an angle so the plane must use the same up as the ground in my case a tileMapgrid
    15.     }
    16.     void Update()
    17.     {
    18.         if (Input.GetMouseButtonUp(0))
    19.         {
    20.             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    21.             float ent;
    22.             if (plane.Raycast(ray, out ent)) //gets the hitpoint on the plane
    23.             {
    24.              
    25.                 var hitPoint = ray.GetPoint(ent);
    26.                 hitPoint.z = hitPoint.z - 0.5f;  //was off by .5
    27.              
    28.              
    29.                 ClickedTile = loadGrid.TopTileMap.WorldToCell(new Vector3(hitPoint.x,0,hitPoint.z)); //the clicked tile
    30.                 Vector3 temp = loadGrid.TopTileMap.CellToWorld(ClickedTile); //convert back to move cursor to position
    31.                 this.transform.position = new Vector3(temp.x, 0, temp.z + 0.5f); //move the cursor... was also off by 0.5f
    32.                 tiledata = loadGrid.GetTilePoint(new Vector2Int(ClickedTile.x, ClickedTile.y), loadGrid.DefaultNoiseSettings); //returns data about the selected tile
    33.             }
    34.          
    35.         }
    36.     }
    37. }