My Player rotates on the Z axis, but not the X axis. Why?
Here is my whole PlayerController C# Script, with it the player can be moved, and it also makes him hover over the ground (It's supposed to be a futuristic bot).
using UnityEngine;
using System.Collections;
public class HoverBot : MonoBehaviour
{
public float speed = 90f;
public float turnSpeed = 5f;
public float hoverForce = 65f;
public float hoverHeight = 3.5f;
private float powerInput;
private float turnInput;
private Rigidbody carRigidbody;
void Awake()
{
carRigidbody = GetComponentInParent<Rigidbody>();
}
void Update()
{
powerInput = Input.GetAxis("Vertical");
turnInput = Input.GetAxis("Horizontal");
}
void FixedUpdate()
{
Ray ray = new Ray(transform.position, -Vector3.up * 10f);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, hoverHeight))
{
float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight;
Vector3 appliedHoverForce = Vector3.up * proportionalHeight * hoverForce;
carRigidbody.AddForceAtPosition(appliedHoverForce, transform.position);
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(transform.forward, hit.normal), 100f * Time.deltaTime);
Debug.Log(hit.normal);
Debug.DrawLine(transform.position, transform.position + appliedHoverForce/100f, Color.red);
}
carRigidbody.velocity = new Vector3(turnInput * turnSpeed, 0f, powerInput * speed);
}
}
My problem is that normally the player rotates automatically to always be parallel to the ground he's hovering above (if he detects a ramp beneath him, he rotates to be parallel to it, which makes it more ergonomic to climb it).
But the Player only rotates on the Z axis, if he tries to climb a ramp on the X axis, thanks to the Debug, I know he detects it, but he does not rotate, why?
Thanks for your time!
Comment