- Home /
Question by
kmedler1 · Apr 07 at 03:50 AM ·
rotationraycasttransformquaternionnavmeshagent
Align navmeshagent to ramp and rotate normally,Script is aligning to slopes but no longer rotating on other axis
Navmesh agent is aligning to ramp but not rotating at all on the y-axis. I need the script to align and rotate. When I disable the script the objects rotate but don't align to ramps and vice versa. Maybe theres a way to disable the script if the ray doesn't detect a ramp? Not sure what the solution would be. Thanks :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AlignRender2 : MonoBehaviour
{
public GameObject ForwardRef;
// Start is called before the first frame update
private void Start()
{
if (ForwardRef == null && transform.parent != null)
{
ForwardRef = transform.parent.gameObject;
}
}
// Update is called once per frame
private void Update()
{
if (ForwardRef == null) { return; }
RaycastHit hit;
if (Physics.Raycast(transform.position, -transform.up, out hit))
{
Vector3 forward = ForwardRef.transform.up;
Debug.DrawRay(transform.position, forward * 3, Color.green);
Vector3 up = hit.normal;
Debug.DrawRay(transform.position, up * 3, Color.blue);
Vector3 project = Vector3.ProjectOnPlane(forward, up);
Debug.DrawRay(transform.position, project * 3, Color.red);
Quaternion rotation = Quaternion.LookRotation(project, up);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, 10 * Time.deltaTime);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Child transforms screwed up by parent's rotations 1 Answer
Have An Object Rotate Around Transform A So That Transform B is Looking at Transform C (C#) 0 Answers
Object slope rotation issue 0 Answers
Unity Rotate Raycast on Quaternion 1 Answer
How to ROTATE an object without slowing the ends (lerp) 3 Answers