# How do I rotate an object based on the terrain?

I am making a kart game like MarioKart and I am shooting 4 rays down from the kart. I have 2 problems.

1: When I shoot the raycasts down with Vector3.down, it shoots downward, but not downward from the kart’s rotation, which is what I need.

2: I want to rotate the kart based on the average of the hitInfos normals, but when I try this it says it can’t convert a Vector3 to a float.

Basically I want the kart to rotate based on the terrain. If anyone could help me with my problems it would be great!

Thanks!

This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KartDriving : MonoBehaviour
{
public Transform kart;
public Vector3 hitInfoAverageNormal;

``````public Vector3 rayFrontStart;
public Vector3 rayLeftStart;
public Vector3 rayBackStart;
public Vector3 rayRightStart;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
rayFrontStart.Set(transform.position.x, transform.position.y - transform.localScale.y / 2, transform.position.z + transform.localScale.z / 2);
rayLeftStart.Set(transform.position.x - transform.localScale.x / 2, transform.position.y - transform.localScale.y / 2, transform.position.z);
rayBackStart.Set(transform.position.x, transform.position.y - transform.localScale.y / 2, transform.position.z - transform.localScale.z / 2);
rayRightStart.Set(transform.position.x + transform.localScale.x / 2, transform.position.y - transform.localScale.y / 2, transform.position.z);
StabalizeKart();
}

void StabalizeKart()
{
//Here we shoot the raycasts underneath the kart
//------------------------------------------------------------------------------------------------

RaycastHit hitInfoFront;
RaycastHit hitInfoLeft;
RaycastHit hitInfoBack;
RaycastHit hitInfoRight;

//Here we shoot the raycasts
Physics.Raycast(rayFrontStart, Vector3.down, out hitInfoFront, 100);
Physics.Raycast(rayLeftStart, Vector3.down, out hitInfoLeft, 100);
Physics.Raycast(rayBackStart, Vector3.down, out hitInfoBack, 100);
Physics.Raycast(rayRightStart, Vector3.down, out hitInfoRight, 100);

//Here we debug the data stored from the raycasts
if (Input.GetKey(KeyCode.Space))
{
Debug.DrawRay(rayFrontStart, Vector3.down);
Debug.Log(hitInfoFront.distance);

Debug.DrawRay(rayLeftStart, Vector3.down);
Debug.Log(hitInfoLeft.distance);

Debug.DrawRay(rayBackStart, Vector3.down);
Debug.Log(hitInfoBack.distance);

Debug.DrawRay(rayRightStart, Vector3.down);
Debug.Log(hitInfoRight.distance);
}

//Here we use the data from the raycasts to rotate the kart
//------------------------------------------------------------------------------------------------
hitInfoAverageNormal.Set(hitInfoFront.normal + hitInfoBack.normal / 2, transform.position.y, hitInfoLeft.normal + hitInfoRight.normal / 2);
kart.rotation = Quaternion.FromToRotation(Vector3.up, hitInfoFront.normal);
``````

Hi there,

First of all, instead of shooting the rays in direction Vector3.down, you must use the player’s down vector. This is made by calling: `-transform.up` (as transform.down does not exist, this is the same).

Probably this also helps in the second question. If it does not work, just use `Quaternion.FromToRotation(transform.up, hitInfoFront.normal)` instead.

Hope it helps.