Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

I need help with raycasting

Discussion in 'Scripting' started by LodestoneNetwork, Jul 20, 2018.

  1. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    I want to find the exact position my raycast hit the terrain, but when i did some testing (placing objects where the raycast points), it seemed like they dont go where my crosshair is.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Raycasting : MonoBehaviour {
    7.  
    8.     public Vector3 pos;
    9.     public Camera camera;
    10.     public float MaxDistance = 10;
    11.  
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.      
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update () {
    20.         Ray ray = camera.ScreenPointToRay(Input.mousePosition);
    21.         RaycastHit hit;
    22.         if(Physics.Raycast(ray,out hit,MaxDistance)) {
    23.             print(hit.point);
    24.             pos = hit.point;
    25.         }
    26.     }
    27. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    If you are using the pos calculated above in line 24 to set an object's position (i.e., in another script than this one) then they should be at the point of contact of the ray with the collider.

    If the collider does not match the visible mesh, that could cause a problem.

    If your prefab or model does not have its zero where you think it should be, that could cause a problem too.

    Put a breakpoint on line 24, see what the value is, then see what the value is in the Unity editor on that emplaced object.
     
  3. KdotDevelopment

    KdotDevelopment

    Joined:
    Oct 2, 2016
    Posts:
    26
    So, what you are saying is to compare the print with the position of the object? I have the object being created in another script with the position pos. I will breakpoint line 24 and see what happens.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    You can also change the code temporarily to only do the raycast when you click on the mouse, then use Debug.Log() to output the value of pos, and compare it to where the GameObject appears to be in the editor after that.
     
  5. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    alright, i stared right at the ground, raycast says i was looking at 325.2, 126.2, 169.7

    The game object appeared at 325.64, 126.2, 170.7. And if im trying to place something up close, that is actually a large distance. it goes to the right of the screen (cant really see it)
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Neat! You have gained intel!

    Now... what next?

    How about: What happens when you replace your spawned object with a single blank gameobject prefab and spawn that instead? Where does it go? Something is moving that GameObject off the original position so might as well make a brand-new game Object with nothing on it and see what happens!
     
  7. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    Instead of my normal object, i set it as an "empty" gameobject. The empty gameobject was very accurate, only by a few decimal places off.
     
  8. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    oh, and here is the code for the other script:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TreeGrowth : MonoBehaviour {
    6.  
    7.     Animator anim;
    8.     public Rigidbody rb;
    9.     public MeshCollider mc;
    10.  
    11.     void Start (){
    12.         anim = GetComponent<Animator>(); // An animation makes the tree grow
    13.         StartCoroutine(Timer());
    14.      
    15.     }
    16.  
    17.     IEnumerator Timer()
    18.     {
    19.         print(Time.time);
    20.         yield return new WaitForSeconds(90); // wait 90 seconds for the tree to grow, so it has the proper tag to be chopped down with an axe
    21.         print(Time.time);
    22.         gameObject.tag = "Tree";
    23.     }
    24.  
    25.  
    26.     void OnTriggerEnter (Collider col){
    27.         if(col.gameObject.tag != "Player") { // So it doesnt freeze on the player
    28.             rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationX;
    29.          
    30.         }
    31.     }
    32. }
    The RigidbodyConstraints.Freeze are so the tree wont move, so if it spawns in the air, it can fall back to the earth and freeze its pos.

    Also, the animation doesn't move it, i just ran a test
     
  9. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    Also, here is where the instantiate command happens:

    Code (CSharp):
    1.  
    2. if(currentSelection == 2) { // making sure the object is selected in the inventory
    3.             if(Input.GetKeyDown(KeyCode.Mouse1) && saplings != 0) {
    4.                     Instantiate(saplingObject,placePos,Quaternion.identity);
    5.                     saplings--; // takes away a sapling (so u cant use it infinitely)
    6.             }
    7.         }
    I checked to see if the placePos was any different from the pos in the other script. it wasnt