- Home /
Attempting to change the slope of a board to match the slope of the terrain below it
I have a board consisting of a three bone armature. Both noses of the board send out a raycast to the terrain below it. The front nose of the board Board-Front brings back the RaycastHit frontHit and the back nose of the board Board-Back brings back the RaycastHit backHit. What I want to happen is: the middle bone Board-Root rotates to the same exact slope as the slope between frontHit and backHit, or the example result in Screen 2. I've tried numerous ways to achieve this result, but screen 1 is really where I've progressed. I deleted past attempts at rotating the board as such in the code below and have just given you what is currently there.
public class ChangeBoardAngle : MonoBehaviour {
RaycastHit frontHit;
RaycastHit backHit;
// Update is called once per frame
void Update ()
{
if (Physics.Raycast(transform.Find("Armature/Board_Root/Board_Front").position, transform.up * -1, out frontHit, 0.1f))
{
if (Physics.Raycast(transform.Find("Armature/Board_Root/Board_Back").position, transform.up * -1, out backHit, 0.1f))
{
Debug.DrawLine(transform.Find("Armature/Board_Root/Board_Front").position, frontHit.point, Color.green);
Debug.DrawLine(transform.Find("Armature/Board_Root/Board_Back").position, backHit.point, Color.green);
}
}
}
}
Answer by fafase · Jul 11, 2014 at 10:48 PM
RaycastHit frontHit;
RaycastHit backHit;
// You should do the Find part in the start once only and use a reference
Physics.Raycast(transform.Find("Armature/Board_Root/Board_Front").position, transform.up * -1, out frontHit, 0.1f);
Physics.Raycast(transform.Find("Armature/Board_Root/Board_Back").position, transform.up * -1, out backHit, 0.1f);
// Find the vector between the two points
Vector3 direction = frontHit.point - backHit.point;
// Create the corresponding Quaternion
Quaternion rotation = Quaternion.LookRotation(direction);
// Apply it the middle board rotation
middleBoardTransform.rotation = rotation;
Your answer
Follow this Question
Related Questions
Nav mesh holes with terrain 1 Answer
Terrain Slope Bouncing 1 Answer
Raycast up slope to find the peak in the terrain. 1 Answer
3d ethan character can't walk up 20 Deg slope 0 Answers
How to create slope roads? 2 Answers