- Home /
GameObject Rotation with KeyDown applied only for a frame.
Hi everybody, I probably have a really dumb question, but I can't seem to figure it out myself, since I'm a bit rusty with coding. Here is the script I'm using:
using UnityEngine;
public class GroundPlacementController : MonoBehaviour
{
public GameObject placeableObjectPrefab;
public GameObject button;
public KeyCode newObjectHotkey = KeyCode.A;
public GameObject currentPlaceableObject;
private float rotation_amount = 90F;
public void Update()
{
if (currentPlaceableObject != null)
{
PlaceObject();
MoveCurrentObjectToMouse();
Rotate();
ReleaseIfClicked();
}
}
public void PlaceObject()
{
currentPlaceableObject = Instantiate(placeableObjectPrefab);
}
public void HandleNewObjectHotkey()
{
if (Input.GetKeyDown(newObjectHotkey))
{
if (currentPlaceableObject != null)
{
Destroy(currentPlaceableObject);
}
else
{
currentPlaceableObject = Instantiate(placeableObjectPrefab);
}
}
}
public void MoveCurrentObjectToMouse()
{
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);
}
}
public void Rotate()
{
if (Input.GetKeyDown(KeyCode.V))
{
currentPlaceableObject.transform.Rotate(0, rotation_amount, 0);
}
}
public void ReleaseIfClicked()
{
if (Input.GetMouseButtonDown(0))
{
currentPlaceableObject = null;
}
}
}
What this script should do is spwan and move a prefab depending on where the mouse cursor is, and then when the V key is pressed, the object should rotate by 90 degrees. The problem is the rotation only works for a split second and then it reverts back to the the original rotation.
Also if you guys could help me out with the system that keeps rotating the object by 90 degrees everytime I press the key down, so for example pressing it 3 times would rotate it by 90,180, 270.
Please help me out guys, I'm loosing my mind here.
Answer by JonPQ · Aug 23, 2019 at 11:39 PM
when key pressed.... use "private float CurrentRotation" variable. CurrentRotation = CurrentRotation + rotation_amount;
then use currentRotation, instead of your delta.. currentPlaceableObject.transform.Rotate(0, currentRotation, 0);
Hi thank you so much for your response. Your solution helps in rotating the object by 90,180 and so on. But I still have the same problem of the Object reverting back to its original position after on frame. Do you have any idea how to solve that? Thanks.
Actually don't worry, I solved it myself by using what you wrote and moving some stuff around. Thank you very much for your help. If anybody else has a more elegant solution to this, I am happy to hear from you.
you can also try RotateAround... where you pass in the axis to rotate around and the delta for how much to rotate by.
I personally like to avoid using angles where possible and just add vectors. For example to rotate right a bit. add a bit of the right transform.vector to the transform.forward vector and the transform will rotate to the right (around the y / up axis). Add a bit of the forward transform.vector to the transform.upvector and the transform will tilt forwards and backwards.
This is great for smoothed or accelerated controls, without need for any fancy math. more intuitive,
Thank you very much for the tip. For this particular I think is fine the way it is. I only need to move a particular object by 90 degrees without a smooth animation or anything fancy.
Your answer
![](https://koobas.hobune.stream/wayback/20220612211032im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Creating a GameObject variable without instantiating it? 1 Answer
Get Y Rotation Between Two Vector3 Points 2 Answers
Make object only rotate in 90 degree increments 0 Answers
The vision of a camera (displayed as a 3D Cone) needs to rotate towards the player 1 Answer