- Home /
Rotating object while button is held
Sorry for saturating the forum, but I couldn't find any results after an hour of searching for a solution. I assume that's why my question was taken down last time without being posted at all, but I really do need some help from anyone willing. I can make it work if I don't hold down the buttons, but I believe that implementing this feature will help make the game more intuitive as opposed to adjusting the angle by 5 degrees every time you press up or down.
Edit: Furthermore, I forgot to mention that when I press the up and down keys, the engine crashes.
Any help is greatly appreciated.
void UpThing()
{
Vector3 direction = new Vector3(0, 0, 1);
targetRotation *= Quaternion.AngleAxis(-5, direction);
}
void DownThing()
{
Vector3 direction = new Vector3(0, 0, 1);
targetRotation *= Quaternion.AngleAxis(5, direction);
}
void Update()
{
if (Input.GetKeyDown("up"))
{
up = true;
}
if (Input.GetKeyUp("up"))
{
up = false;
}
while(up)
{
InvokeRepeating("UpThing", 1f, 1f);
}
if (Input.GetKeyDown("down"))
{
down = true;
}
if (Input.GetKeyUp("up"))
{
down = false;
}
while (down)
{
InvokeRepeating("DownThing", 1f, 1f);
}
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, 10 * smooth * Time.deltaTime);
}
Answer by ADiSiN · Jul 12, 2019 at 03:58 AM
Hello!
So, I believe that the reason why your engine crashes it's because when you set up or down boolean to true the engine each update invoke repeating and it stucking on each other forever so it crashes.
Try this code:
public float speed = 2f;
bool b_IsRotating = false;
Vector2 v2_Movement;
Quaternion q_StartRotation;
private void Start()
{
q_StartRotation = transform.rotation;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
b_IsRotating = true;
if (Input.GetMouseButtonUp(0))
b_IsRotating = false;
if (b_IsRotating)
{
v2_Movement.Set(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"));
transform.Rotate(v2_Movement * speed, Space.Self);
}
if (Input.GetMouseButtonDown(1))
transform.rotation = q_StartRotation;
}
So, we have speed to adjust speed rotation, b_IsRotating to see if we should track mouse input change and store it in v2_Movement. Also we save start rotation to q_StartRotation just so palyer wil be able to quick reset rotation. When you press left mouse button you set boolean to true and when we release it then we change it to false. When we press right mouse button we reset the rotation. If you have qny question regarding to this script - ask. You can improve it by storing rotating object in variable, because currently you should put this script on each object that you want to rotate what's not good :)
Thank you so much for your reply! It gave me the hint I needed to solve this thing.
Answer by bgoldbeck · Jul 12, 2019 at 03:15 AM
Try using GetKey instead of GetKeyUp/Down
Thank you for cleaning up my code; however, this doesn't stop the engine from crashing.
Your answer

Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Realtime cubemap that respects rotation? 0 Answers
Velocity bug with hinge joints forces in C# 0 Answers
transform.rotation cancels movement. 1 Answer
Network.Instatiate Problem 0 Answers