- Home /
Object rotation snaps back to 0°
As you can see whenever i press "e" to rotate the object 90° on the y axis it keeps snapping back to 0° regardless of if i even turn it -90°. it only would turn of i put the object at the very corners of the ground then it rotates but when i drag the object away from the edges it will snap back to 0°. How do i solve this.
This is the Code for the rotation of the currently held object, simple.
if (Input.GetKeyDown ("e"))
{
currentPlaceableObject.transform.Rotate(transform.rotation.x, transform.rotation.eulerAngles.y + 90, transform.rotation.z);
}
else if (Input.GetKeyDown ("q"))
{
currentPlaceableObject.transform.Rotate(transform.rotation.x, transform.rotation.eulerAngles.y - 90, transform.rotation.z);
}
It doesn't need to use 2 keys it could just be so that whenever i press "r"it will successfully rotate a full 90°.
is that the complete code that manipulates the transform in question?
You definitely have a problem somewhere else in your code, please edit and post your entire script
[SerializeField]
private GameObject placeableObjectPrefab;
[SerializeField]
private $$anonymous$$eyCode newObjectHotkey = $$anonymous$$eyCode.A;
private GameObject currentPlaceableObject;
private float mouseWheelRotation;
private void Update()
{
HandleNewObjectHotkey();
if(currentPlaceableObject != null)
{
$$anonymous$$oveCurrentPlaceableObjectTo$$anonymous$$ouse();
$$anonymous$$ainRotation();
ReleaseIfClicked();
}
}
private void ReleaseIfClicked()
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
currentPlaceableObject = null;
}
}
private void $$anonymous$$ainRotation()
{
if (Input.Get$$anonymous$$eyDown ("e"))
{
currentPlaceableObject.transform.Rotate(transform.rotation.x, transform.rotation.eulerAngles.y + 90, transform.rotation.z);
}
else if (Input.Get$$anonymous$$eyDown ("q"))
{
currentPlaceableObject.transform.Rotate(transform.rotation.x, transform.rotation.eulerAngles.y - 90, transform.rotation.z);
}
}
private void $$anonymous$$oveCurrentPlaceableObjectTo$$anonymous$$ouse()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if(Physics.Raycast(ray, out hitInfo))
{
currentPlaceableObject.transform.position = hitInfo.point;
currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
}
}
private void HandleNewObjectHotkey()
{
if(Input.Get$$anonymous$$eyDown(newObjectHotkey))
{
if(currentPlaceableObject == null)
{
currentPlaceableObject = Instantiate(placeableObjectPrefab);
} else {
Destroy(currentPlaceableObject);
}
}
}
Looks like you are setting the rotation back to normal every frame:
if(Physics.Raycast(ray, out hitInfo))
{
currentPlaceableObject.transform.position = hitInfo.point;
currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
}
Try taking that rotation line out, see what happens
Answer by Riccardoric · Jan 14, 2020 at 03:44 PM
Hello,
When you use Rotate Unity will take the current position and add the vector3 passed as parameter. Try remove "transform.rotation." with 0f, then just on the axis that you want rotate insert the degrees that you want.
Something like that ->
if (Input.GetKeyDown ("e"))
{
currentPlaceableObject.transform.Rotate(0f, 90f, 0f);
}
else if (Input.GetKeyDown ("q"))
{
currentPlaceableObject.transform.Rotate(0f, -90f, 0f);
}
I hope I helped you. Greetings
Your answer
![](https://koobas.hobune.stream/wayback/20220612221512im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can I use GetComponents reference outside Of awake and start functions 1 Answer
C# - Doesn't Unity support Expression Bodied Properties? 3 Answers
Scrip cannot work after reloading the scene or reopen Unity 0 Answers
Load Google Play Popup during Unity startup on Android? 1 Answer
"on" and "off" the effect. 0 Answers