- Home /
Aligning an Object to a Surface, and Rotating it
I am trying to align my player to the surface of an object. This part I understand how to do simply with Quaternion.FromToRotation(Vector3.up, hitInfo.normal.up); using raycasts and hitInfo being the Raycast variable. However this doesn't give me the ability to rotate the object afterwords. I need to be able to adjust the y rotation of my player since that is how you steer it. I am not really familiar with how the Quaternion function from above really works, and if I did that might help get this to work. I would really appreciate any help. Thanks!
Answer by JinJin · May 08, 2015 at 12:31 PM
this should give you an idea:
Vector3 eulerAngles = transform.rotation.eulerAngles;
Mathf.LerpAngle( eulerAngles.y, myCameraTransform.rotation.eulerAngles.y, rotSpeed * Time.deltaTime );
transform.rotation.eulerAngles = eulerAngles;
this codes takes your current rotation and changes only the y part based on the camera
Answer by Saddamjit_Singh · Sep 04, 2017 at 07:49 AM
using UnityEngine;
public class SlopeDetectionScript : MonoBehaviour
{
RaycastHit hit;
Quaternion rot;
public int smooth;
void Update()
{
Ray ray = new Ray(transform.position, -transform.up);
if (Physics.Raycast(ray, out hit))
{
rot = Quaternion.FromToRotation(transform.up, hit.normal) transform.rotation;
transform.rotation = Quaternion.Lerp(transform.rotation, rot, Time.deltaTime smooth);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613194900im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Rotate Raycast on Quaternion 1 Answer
How do I get an object to rotate around a different axis based on the raycasts hit angle? 0 Answers
character wont rotate and is stuck to the one position. 0 Answers
GameObject facing hit.point doesn't always work 1 Answer
How to get rotation relative to the ground normal? 0 Answers