How can i make dynamic character orientation with C#?

Hello there. I am making an game and i want to know how can i make dynamic character orientation in Unity.

Basically i want to snap the characters rotation to the ground

Like this :

The character should climb up slopes or hills like that

Can someody help me ?

You can try something like this

float angle = 24f;
// Distance along the normal direction
float distance= 0f;

GameObject obj;

RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000))
{
    Vector3 normal = hit.normal;

    Quaternion q1 = Quaternion.AngleAxis(rotation, normal);
    Quaternion q2 = Quaternion.FromToRotation(Vector3.up, normal);
    Quaternion quat =  q1 * q2;                   

    obj.transform.position = hit.point + normal * distance;
    obj.transform.rotation = quat;
}

Thanks ! I’ll try to make this work beacause it’s showing some errors, beacause ‘ray’ and ‘rotation’ is not included. But thanks anyways :slight_smile:

here is a complete code for you

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

public class AlignToNormal : MonoBehaviour {

    public float angle = 0f; //you can use this to tweak the angle
    // Distance along the normal direction
    float distance = 0f;


    void Update()
    {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, 1000))
        {
            Vector3 normal = hit.normal;

            Quaternion q1 = Quaternion.AngleAxis(angle, normal);
            Quaternion q2 = Quaternion.FromToRotation(Vector3.up, normal);
            Quaternion quat = q1 * q2;

            transform.position = hit.point;
            transform.rotation = quat;
        }
    }
}