I just recently applied code that would make it so that the character would terrain hug i.e. change angle according to the angle of the terrain underneath them. The problem is that I'm terrible with vector and angles and can't figure out why this takes precedent over my ability to rotate the character.
Transform cameraTransform = Camera.main.transform;
Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
Vector3 right = new Vector3(forward.z, 0, -forward.x);
if (isControllable)
{
v = Input.GetAxis("Vertical");
h = Input.GetAxis("Horizontal");
}
targetDirection = h * right + v * forward;
// Smooth the speed based on the current target direction
// Choose target speed
//* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0f);
//moveDirection = Vector3.zero;
Vector3 moveDirection = transform.TransformDirection(Vector3.forward);
if (targetDirection != Vector3.zero)
{
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
}
float verticalSpeed = 0.0f;
Vector3 movement = moveDirection * moveSpeed + new Vector3(0, verticalSpeed, 0);
movement *= Time.deltaTime;
// this line seems to control rotating and moving towards rotated direction
transform.rotation = Quaternion.LookRotation(moveDirection);
////////////////////////////////////////////////////////ROTATE TOWARDS GROUND//////////////////////////////
Ray ray = new Ray(transform.position, -transform.up);
//RaycastHit hit;
Physics.Raycast(ray,out hit);
if(hit.normal != Vector3.zero)
{
Quaternion newRot = Quaternion.FromToRotation (Vector3.up, hit.normal);
transform.localRotation = Quaternion.Slerp(transform.localRotation, newRot, 0.5f);
}
If I have the "Rotate towards Ground" code at the very bottom of this function (called in Update) the character only walks with Y equaling 0 exclusively. It behaves properly aside from that.
However, if I move the rotating code underneath the 'Rotate towards ground' code, now I can rotate, but I don't angle towards the ground at all. I'm unsure how to keep the y axis from being affected but still allowing terrain hugging.
////////////////////////////////////////////////////////ROTATE TOWARDS GROUND//////////////////////////////
Ray ray = new Ray(transform.position, -transform.up);
//RaycastHit hit;
Physics.Raycast(ray,out hit);
if(hit.normal != Vector3.zero)
{
Quaternion newRot = Quaternion.FromToRotation (Vector3.up, hit.normal);
transform.localRotation = Quaternion.Slerp(transform.localRotation, newRot, 0.5f);
}
// this line seems to control rotating and moving towards rotated direction
transform.rotation = Quaternion.LookRotation(moveDirection);
I've tried picking out only the z and x axis to be affected by ground hugging rotation but it gives weird results/no results at all.
↧