i’m not sure if barycentric coordinates are what i’m looking for but i tried some code and it seems work somewhat.
in the road angle code, when i go up a hill and turn, the angle is not rotating correctly.
also when i apply going up a hill angle steep enough the angle of force is in the world coordinates.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class carController : MonoBehaviour {
public float acceleration;
public float rotationRate;
public float turnRotationAngle;
public float turnRotationSeekSpeed;
private float rotationVelocity;
private float groundAngleVelocity;
private float ROTvel;
private float rotvel2;
void Update(){
}
// Update is called once per frame
void FixedUpdate () {
if (Physics.Raycast(transform.position, transform.up * -1, 5f))
{
//Quaternion trot = Quaternion.FromToRotation(transform.up*-1, hit.barycentricCoordinate.normalized);
//transform.rotation = Quaternion.SlerpUnclamped(gameObject.transform.rotation,trot,1*Time.deltaTime);
Vector3 forwardForce = transform.forward * acceleration * Input.GetAxis("Vertical");
forwardForce = forwardForce * Time.deltaTime * 1000;
gameObject.GetComponent<Rigidbody>().AddForce(forwardForce);
}
else
{gameObject.GetComponent<Rigidbody>().drag = 0;
}
Vector3 turnTorque = Vector3.up * rotationRate * Input.GetAxis("Horizontal");
turnTorque = turnTorque * Time.deltaTime * gameObject.GetComponent<Rigidbody>().mass;
gameObject.GetComponent<Rigidbody>().AddTorque(turnTorque);
Vector3 newRotation = transform.eulerAngles;
newRotation.z = Mathf.SmoothDampAngle(newRotation.z, Input.GetAxis("Horizontal")*-turnRotationAngle, ref rotationVelocity, turnRotationSeekSpeed);
//gameObject.GetComponent<Rigidbody>().rotation.eulerAngles.Equals(newRotation);
//rotate to road angle
RaycastHit hit;
Physics.Raycast(transform.position, transform.up * -1, out hit, 10f);
ROTvel = gameObject.GetComponent<Rigidbody>().angularVelocity.y;
rotvel2 = gameObject.GetComponent<Rigidbody>().angularVelocity.x;
newRotation.x = Mathf.SmoothDampAngle(newRotation.x, hit.barycentricCoordinate.normalized.x, ref ROTvel, 0.01f);
newRotation.y = Mathf.SmoothDampAngle(newRotation.y, hit.barycentricCoordinate.normalized.y, ref ROTvel, 0.01f);
gameObject.GetComponent<Rigidbody>().rotation.eulerAngles.Equals(newRotation);