Snap Object always to 90 degree values?
Hey! I am working on a level editor where you can place some object and also rotate them - here is how it looks like atm: https://twitter.com/Der_Kevin/status/669233275001524224 what i am trying right now is that the rotation should always snap to 90 degree steps. so that the object Y rotation is either 0°,90°,180° or 270° how can i let the objects snap to these positions?
Answer by Cherno · Jan 19, 2016 at 02:11 PM
public Vector3 lookAtTarget;//the point in world space where the object looks at (not snapped)
Vector3 aimingDir = lookAtTarget - transform.position
float angle = -Mathf.Atan2(aimingDir.z, aimingDir .x) * Mathf.Rad2Deg + 90.0f;
angle = Mathf.Round(angle / 90.0f) * 90.0f;
Quaternion qTo = Quaternion.AngleAxis(angle, Vector3.up);
transform.rotation = qTo;
i tried it like that:
using UnityEngine;
using System.Collections;
public class RotationSnap : $$anonymous$$onoBehaviour {
public Vector3 lookAtTarget;//the point in world space where the object looks at (not snapped)
public Vector3 ai$$anonymous$$gDir;
public float angle;
// Update is called once per frame
void Update () {
Vector3 ai$$anonymous$$gDir = lookAtTarget - transform.position
angle = -$$anonymous$$athf.Atan2(ai$$anonymous$$gDir.z, ai$$anonymous$$gDir .x) * $$anonymous$$athf.Rad2Deg + 90.0f;
angle = $$anonymous$$athf.Round(angle / 90.0f) * 90.0f;
Quaternion qTo = Quaternion.AngleAxis(angle, Vector3.up);
/ transform.rotation = qTo;
}
}
but it somehow gave me a bunch of errors?
Answer by brunopava · Jan 19, 2016 at 02:06 PM
Edit > Snap Settings on menu.
He wrote that he is working on a level editor, not using the Unity Editor, so he won't be able to make use of the snap settings.
Your answer
![](https://koobas.hobune.stream/wayback/20220612060815im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
,Snap rotation relative to rotation 1 Answer
Rotation and Gravity Relative to the Center of an Object 0 Answers
With VR character's rotation is applied twice or double 1 Answer
Using LookAT causes no rotation at all? 1 Answer
rotation problems 0 Answers