Rotate object (91842)

Hey there everyone,

I’ve ran in to a problem: I have a Prefab that follows my mouse and I am trying to rotate it (around the Y-axis). I’ve looked everywhere and tried everything but my prefab keeps spinning around in big circles (not around the Y-axis).
This is my current rotation code:

    if(Input.GetKey(KeyCode.R)){
    				Instance.transform.Rotate(0,10,0);
    			}
`

Is anyone able to help me out?
Note: the script is attached to my camera, I think it might have something to do with this (?)
`

Edit: I fixed the script that causes my object to follow the cursor, it’s working properly now. Thanks all!

1 Answer

1

Script needs to be attached to your object, not the camera. Plus you should use Time.deltaTime:

function Update() {
    if(Input.GetKey(KeyCode.R)){ 
        transform.Rotate(0,60.0 * Time.deltaTime,0);
    } 
}

So it wouldn't be possible to have it on a different object?

–

You can, but you have to reference the game object you are spinning. Lets say the object you want to spin has the name 'Player', you could do: var player : Transform; function Start() { player = GameObject.Find("Player").transform; } function Update() { if(Input.GetKey(KeyCode.R)){ player.Rotate(0,60.0 * Time.deltaTime,0); } }

–

I am using the following code to reference the GameObject. <pre>public GameObject Instance;and</pre><pre> Instance = Instantiate(InstancePrefab, camera.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity) as GameObject;<code> </pre></code>

–

Then the rotation code would be: function Update() { if(Input.GetKey(KeyCode.R)){ Instance.transform.Rotate(0,60.0 * Time.deltaTime,0); } }

–

It does not seem to work. I get the same result as always: http://i.imgur.com/U1hHxvU.gif It just starts making circles.

–