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. Dismiss Notice

Raycast for strategy game not working

Discussion in 'Scripting' started by Vire7777, Dec 31, 2020.

  1. Vire7777

    Vire7777

    Joined:
    Mar 13, 2019
    Posts:
    37
    Hello everyone

    I have an issue with a raycast
    I did the following

    Code (CSharp):
    1. Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    2. RaycastHit hitData;
    3. if (terrainCollider.Raycast(ray, out hitData, 200))
    4. {
    5.      buildingToCreate.position = hitData.point;
    6. }
    My problem is that my building is never exactly where my pointer is, or rarely
    It s like if there was a mistake increasing or decreasing depending of the position

    here it s almost (but not totally) good

    upload_2020-12-31_1-43-45.png

    Here it s worst

    upload_2020-12-31_1-47-6.png

    Any idea how to fix this ?
    I have the impression it s maybe due to my camera

    upload_2020-12-31_1-47-57.png

    Thanks for your help and time
    Vivi
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    I would forego the impressions and start sticking some debugging instrumentation in there to print data out in realtime, then pause the editor and study where the house ends up, see what varies.

    In general to track down these sorts of things, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  3. Vire7777

    Vire7777

    Joined:
    Mar 13, 2019
    Posts:
    37
    Well i did Debug.log. I m not a newbie :p
    and yes the code is running, and is in the update method
    It s running everyframe then

    So when i m moving my cursor, my house is following my mouse but with this distance increasing or decreasing depending of the position of my cursor and that s annoying

    i really don t get why.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You say that, but you did not find any helpful information doing so, meaning the advice is definitely on point. You are the one who has the code. You are the only one who can get further information about why it is doing what it is. Otherwise feel free to post a minimal working example we can copy paste and reconstruct which shows the problem. Then people, who are nice enough to do the work for you, could also debug it. Otherwise that's something only you can do :)

    Not true at all. Does this run every frame?
    Code (CSharp):
    1. // Inside Update()
    2. if(false){
    3.     // Never runs!
    4. }
    No, i'm not joking. Something like this can literally happen (ideally not because you literally wrote 'false' there, but still). And that's what @Kurt-Dekker meant when he said to find out how often code is running. You posted a very minimal code example. Which is mostly out of the documentation. So that's likely not the culprit, meaning the problem is likely somewhere else in your code, or project hierarchy, where we have no information about.
     
    Last edited: Jan 1, 2021
    Kurt-Dekker likes this.
  5. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    What happens if you try using Physics.Raycast instead of terrainCollider.Raycast?
     
  6. payalzariya07

    payalzariya07

    Joined:
    Oct 5, 2018
    Posts:
    85
  7. Vire7777

    Vire7777

    Joined:
    Mar 13, 2019
    Posts:
    37
    Hello and thanks for your answer but that is actually what i am using as written in my first post

    TerrainCollider terrainCollider = Terrain.activeTerrain.GetComponent<TerrainCollider>();
    if (terrainCollider.Raycast(ray, out hitData, 200))


    If i use Physics.Raycast, my house is flying closer and closer to my camera going from down to up and the doing it again
    I suppose because it puts itself each frame on the top of its last position and going like this upper and upper until reaching the camera



    Yoreki + Kurt, you are surely right but i don t know what more i could post you because there is nothing more than this in the class
    buildingToCreate is intialized with a prefab at first and the terrain is already here.
    So i would love to tell you more or give you more informations but i don t know how
    I was just trying to follow this video

    But for me doesn t do the same and i don t get why
     
  8. Vire7777

    Vire7777

    Joined:
    Mar 13, 2019
    Posts:
    37
    here with the Physics raycast

    and then the terraincollider raycast
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Ha I know that guy, Jason Weimann.. he does great stuff.

    As you noted, it looks like in the first one (Physics Raycast) the house is hitting itself again and again and placing itself higher up to the camera.

    Using the terrain collider is best probably. Try replace your house with a Unity Cube Primitive and see where it ends up. If it isn't offset, then you have an offset in your house prefab. If the cube is offset as much as the house, then there's a bug, perhaps somewhere in the scaling possibly. ALL scale values should be (1,1,1) except for GameObjects that have zero children (eg. "leaves").
     
  10. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    With the physics raycast youll have to set the layermask of your terrain and raycast layermask inorder for it to only cast for the terrain.

    For the physics raycast vid, even in the first frame, the house was offset by a set amount. in the terrainCollider raycast vid, the raycast seems fixed aswell. best if you try what @Kurt-Dekker recommended first.

    Assuming what we see of the house is the collision of the house though, this does not explain why your physics raycast would detect the house with how far the mouse was from the house collision.
     
  11. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    turn off the collider on the "blueprint", very simple solution to this problem, heh
    what i do with build menus like this is just take the renderer with a special transparent material to show where the building will be and when the player clicks build it just tells the game "build this building here" and the game spawns a new building there without caring what you did before (also makes it so the AI and the player use the same function to build buildings on the map)
    after that if you hold shift - carry on building the same blueprint, if not clear out the "active building".
     
  12. Vire7777

    Vire7777

    Joined:
    Mar 13, 2019
    Posts:
    37
    Thanks a lot for your help
    So i tried the cube and... same problem exactly


    I don t use scales, they are all at 1
    I mean it s only a terrain, a camera and a cube
     
  13. Vire7777

    Vire7777

    Joined:
    Mar 13, 2019
    Posts:
    37
    So i realised that if i put my cursor in the down left corner, then the cube is aligned to my cursor
    but only there
     
  14. Vire7777

    Vire7777

    Joined:
    Mar 13, 2019
    Posts:
    37
    Tried also but it doesn t work even if i put a layer on my terrain and in the function like this
    if (Physics.Raycast(ray, out hitData, 1000, 11))
    with terrain layer set a 11
     
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    There are other flavors of raycast you can use. For instance if there is only one collider (Say the TerrainCollider), then you can raycast ONLY against that.

    Instead of Physics.Raycast (a static method that transacts against the entire Physics system), you would use Collider.Raycast, which takes a single instance of a collider and raycasts to it.

    Alternately to avoid going too off-track from the tutorial, back up and go over the parts related to the collider. You clearly have a problem with one of the steps in the process.
     
  16. Vire7777

    Vire7777

    Joined:
    Mar 13, 2019
    Posts:
    37
    Thanks so much for your help
    I really think the problem was from the camera because when i used the main camera (which is in 1st person view) it works perfectly

    So i decided to change a bit how to create my buildings and make it a first person view instead of doing so on the minimap. And that works.

    Still i really thank you and i m a bit sad i didn t find why but i spent too much time on it, and at some point you have to decide what to do... still running or finding an other way.
     
    Kurt-Dekker likes this.