Search Unity

Raycasting IK targets to ground gets them stuck on terrain

Discussion in 'Animation' started by bebros2, Jun 29, 2022.

  1. bebros2

    bebros2

    Joined:
    Aug 28, 2021
    Posts:
    36
    I was trying to mimic the spider effect I see in some videos where they raycast the legs onto the ground and make them move but I'm stuck on just raycasting them to the ground. I'm using unity's built in terrain and whenever the targets go on uneven terrain the legs get stuck at a certain position off the ground.

    I've attached a gif of the issue I'm experiencing and here is the code I'm using to raycast.

    Code (CSharp):
    1. if (Physics.Raycast(RightLegTarget3.transform.position, Vector3.down, out hit, distance))
    2. {
    3.       Vector3 targetLocation = hit.point;
    4.  
    5.       targetLocation += new Vector3(0, RightLegTarget3.transform.localScale.y / 2, 0);
    6.  
    7.       RightLegTarget3.transform.position = targetLocation;
    8. }
    I'm repeating this for all six legs in FixedUpdate which I don't think is a good thing, but I don't know another way to handle lots of raycasts. The issue seems to happen less when the body is higher up and I've tried raycasting the body above the ground, but it always ends up jittering around. I appreciate any help and I hope that I put this in the right forum section.
     

    Attached Files:

  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Do the spheres have colliders? The raycasts might be hitting those. Otherwise, try using Debug.DrawLine and Debug.Log to figure out what it's actually hitting.