- Home /
Help with collisions with terrain
I have a sprite that has a follow script that simply rotates with the previous section. Simple Tree view from Pascal days.
But, I think my raycast is off. In the video I can rotate into the terrain. I want any sprite to not move into the terrain. I think my raycast is way off.
I have mesh collider with a character controller on each. Or should I just use a rigidy body on the following parts
Code:
IEnumerator LerpPosition(Vector3 targetPosition, float duration, Quaternion toRotation)
{
float time = 0;
Vector3 startPosition = transform.position;
while (time < duration)
{
transform.position = Vector3.Lerp(startPosition, targetPosition, time / duration);
// ray cast to see if it terrain, if so exit
if(CheckRaycast(transform.position, toRotation * Vector3.forward, 5f))
{
yield return null;
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, _rotationSpeed * Time.deltaTime);
time += Time.deltaTime;
yield return null;
}
transform.position = targetPosition;
_shouldMove = false;
if (ChildPlayer != null)
{
var cFollow = ChildPlayer.GetComponent<PlayerFollow>();
if (cFollow != null)
{
var i = transform.GetComponent<RectTransform>();
cFollow.Move(GetBehindPosition(transform, (((transform.GetComponent<RectTransform>().sizeDelta.x * i.sizeDelta.magnitude))*2f) ), _toRotation, _rotationSpeed);
}
}
}
Here is the Inspector. i'm lerping it sideways and I think my raycast direction may be wrong. I need to raycast the it's being rotated.
In the video I'm hitting the left arrow to rotate and they all follow the rotation from the body part in front.
If the body part is going to rotate into the terrain I want to stop the lerp. Maybe the way i'm exiting the Coroutine is wrong?
Yield return null doesn't exit out of your coroutine, it just yields its time until the next update cycle, then will resume running from that same spot. If you want to exit out of the coroutine, use yield break instead.
Your answer
![](https://koobas.hobune.stream/wayback/20220613045515im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Place character on the ground 1 Answer
Hilarious Gravity defect i don't know how to fix 1 Answer
NPCs fall through hills in terrain 1 Answer
Import problem with Blender3d 2 Answers
Character Controller falling through ALL mesh colliders. 0 Answers