I want to make a third person character rotate so that its transform.up is the same as the normal vector of the floor it is standing on, and I am pretty much completely lost. here is my script, any help would be great.
using System;
using Unity.VisualScripting;
using UnityEditor.Callbacks;
using UnityEngine;
using UnityEngine.Animations;
public class player : MonoBehaviour
{
public float speed;
public float MaxFloorAngle;
Vector3 floorAngle;
Rigidbody rb;
public Transform Cam;
bool grounded;
Vector3 moveDir;
public float JumpHeight;
float mouseX, mouseY;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
Cursor.lockState = CursorLockMode.Locked;
}
float xRotation;
float desiredX;
// Update is called once per frame
void Update()
{
GetInput();
Look();
}
private void Look()
{
//transform.localRotation = Quaternion.Euler(0, Cam.eulerAngles.y, 0);
}
void FixedUpdate()
{
if(grounded)
{
rb.AddForce(moveDir * speed, ForceMode.Force);
rb.drag = 6;
}else
{
rb.AddForce(moveDir * speed / 3, ForceMode.Force);
rb.drag = 2;
}
if(grounded && Input.GetButton("Jump"))
{
rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
rb.AddForce(Vector3.up * JumpHeight, ForceMode.Impulse);
}
transform.localRotation = Quaternion.Euler((floorAngle * Mathf.Rad2Deg).x, (floorAngle * Mathf.Rad2Deg).y * Cam.eulerAngles.y, (floorAngle * Mathf.Rad2Deg).z);
}
private void GetInput()
{
moveDir = (transform.forward * Input.GetAxis("Vertical") * speed + transform.right * Input.GetAxis("Horizontal") * speed);
grounded = Physics.Raycast(transform.position, Vector3.down, 0.1f);
mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * 400;
mouseY = Input.GetAxis("Mouse Y") * Time.deltaTime * 400;
RaycastHit hit = new RaycastHit();
Ray ray = new Ray(transform.position, Vector3.down);
if(Physics.Raycast(ray, out hit))
{
floorAngle = hit.normal;
Debug.Log(floorAngle * Mathf.Rad2Deg);
}
}
}
thanks!