- Home /
Rotate a cloned object with key press
I'm trying to rotate a cloned object once it is spawned but i'm having difficulty and cant find any information about how to do this.
For example, i spawn a wall, once i press "R" it rotates to 90 degrees along its x axis. Or, if you press "R" it transforms to 90 and then you put it down? I just need an understanding on how to do this. I'm unfamiliar with Quaternions is this the problem?? The code i have doesn't seem to be working.
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Storage point for the Ray
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
// Assigning the point its variable
endPoint = hit.point;
GameObject clone;
// Clone the objects and spawn them in the game 'world'
clone = Instantiate(items[i], endPoint, items[i].transform.rotation) as GameObject;
if (Input.GetKey(KeyCode.R))
{
clone.transform.Rotate(0, 90, 0);
Debug.Log("Rotating Object");
}
}
}
The object isn't rotating when i press R, maybe creating a new transform function will work?
Answer by HarshadK · Feb 20, 2015 at 02:03 PM
As per your code the R key has to be pressed down in the same frame when the left mouse button is clicked.
Just take out the code that handles the R key press outside Mouse Button press check and change your code accordingly.
You need to declare your 'clone' gameobject to be outside Update in order for the object cloned to persist between multiple frame executions. Then on R keypress check if clone is null or not and if not null you can rotate it with the current rotation code. Also once the object is placed on the ground just set your clone object to null in order to avoid that object being rotated even after it is placed on the ground.
Thank you, works great! Only just realized that it was because it was within the button down function.
Your answer
Follow this Question
Related Questions
Clone Selector 1 Answer
Instantiate GameObject cloned into wrong position. 1 Answer
Assign an instantiated GameObject? 1 Answer
Reference an Instance of an Object?C# 1 Answer
Variable being shared between clones 2 Answers