0

I'm coming across a roadblock on something I thought would be a relatively simple problem. I want to "roll" the camera on the z-axis by pressing the "Q" and "E" keys.

Here is the code I've written, which is attached to my camera object:

#pragma strict

var keyboardSensitivity : float = 10.0f;
private var rotZ : float;
private var localRotation : Quaternion;

function Start () {
    rotZ = 0.0f;
}

function Update () {
    if(Input.GetKey(KeyCode.Q)) {
        rotZ += Time.deltaTime * keyboardSensitivity;
        localRotation = Quaternion.Euler(0.0f, 0.0f, rotZ);
        transform.rotation = localRotation;
    }
    if(Input.GetKey(KeyCode.E)) {
        rotZ -= Time.deltaTime * keyboardSensitivity;
        localRotation = Quaternion.Euler(0.0f, 0.0f, rotZ);
        transform.rotation = localRotation;
    } 
}

Based on my knowledge, this should be all that is needed. But when I hit the Q or E keys, absolutely nothing happens. Why?

JavascriptLoser
  • 1,853
  • 5
  • 34
  • 61
  • Please don't say "nothing happens". This code should not even compile because `localRotation` is not declared. It's worth noting that [Unityscript](https://stackoverflow.com/questions/45523239/is-unityscript-javascript-discontinued?s=1|5.6699) is discontinued. You may want to switch to start asking C# questions. – Programmer Aug 29 '17 at 10:20
  • I say "nothing happens" because literally nothing happens. I forgot to copy across `localRotation` when I was asking this question but the code certainly compiles fine. I use Unityscript as a personal preference, I don't believe that not using C# is the cause of the problem I'm facing now but I'll keep that in mind for future questions. – JavascriptLoser Aug 29 '17 at 10:24
  • I've seen people post a code that doesn't even compile here. So I though it was one of those. I mentioned C# as a notice for your future questions. – Programmer Aug 29 '17 at 10:38

1 Answers1

0

Nothing happens because your code is likely not attached to the camera or it is attached to another GameObject. It cannot be attached to another GameObject. It has to be attached to the camera since you are referencing transform.rotation which will affect the current GameObject the script is attached to.

Select your camera then drag the script to it. Click "Play" and press the Q or E button. The camera should rotate. I really do recommend Unity project tutorials to you.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I found the problem to be another piece of code interfering with the z-rotation, but I will mark yours as accepted. – JavascriptLoser Aug 29 '17 at 10:41
  • That's another possible reason if the z is being changed from another script otherwise, the only reason the code in your question may not work is because it is not attached to the camera or attached to another gameobject. – Programmer Aug 29 '17 at 10:43