x


rotating an object only on z-axis help - c#

I'm attempting to adjust the barrel of a gun up and down (to show it's going fire a further distance)

I can get it to rotate just fine, but i only want it to rotate with in a certain radius otherwise you can just keep flipping it.

if  (Input.GetKey(KeyCode.S) && gun.transform.rotation.z >= 350 || gun.transform.rotation.z <= 20))
gun.transform.RotateAroundLocal(Vector3.back, (rotateRate * Time.deltaTime) * -1f);

this doesn't seem to work. I thought maybe the rotation would be in radians (that broke things) so i believe my math is messed up..

so this is what i want to do, have a gun that moves down when you press S and if it goes to 350 (after wrapping past 0,) it stops..

on the other side, i would like to do the opposite with W where if it goes from 350 (wrapping past 0) to 20 it stops at 20.

more ▼

asked Jan 03 '12 at 08:41 PM

kievar1983 gravatar image

kievar1983
66 10 11 15

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

i've changed how i am checking my code, i'm not using a second rotation to set it, cause it's not catching it, i also threw in some variables to check for wrapping. I also built in a boolean catch to try and stop it, some time it works some times i can get past it, some times it doesn't catch it at all...

this is my full update code for it.

if(Input.GetKeyDown(KeyCode.W))
       {
         rotate = true;
       }
       if (Input.GetKeyDown(KeyCode.S))
       {
         rotate = true;
       }
       if (rotate)
       {

         if  (Input.GetKey(KeyCode.S) && (gun.transform.rotation.eulerAngles.z >= 350 || gun.transform.rotation.eulerAngles.z <= 20))
         {
              if (wrapped == false && tooFar == true && Input.GetKey(KeyCode.S))
              {
                 tooFar = false;
              }
              if (gun.transform.rotation.eulerAngles.z <= 0 && Input.GetKey(KeyCode.S))
              {
                 wrapped = true;
              }
              if (gun.transform.rotation.eulerAngles.z <= 350 && wrapped == true && Input.GetKey(KeyCode.S))
              {
                 tooFar = true;
              }
              else
              {
                 tooFar = false;
              }

              if (tooFar == false)
              {
                 gun.transform.RotateAroundLocal(Vector3.forward, (rotateRate * Time.deltaTime) * -1f);
              }
              if (tooFar == true)
              {
                 rotate = false;
              }
         }
         else if  (Input.GetKey(KeyCode.W) && gun.transform.rotation.eulerAngles.z <= 350 || gun.transform.rotation.eulerAngles.z >= 20)
         {
              if (gun.transform.rotation.eulerAngles.z >= 359 && Input.GetKey(KeyCode.W))
              {
                 wrapped = false;
              }
              if gun.transform.rotation.eulerAngles.z >= 20 && Input.GetKey(KeyCode.W) && wrapped == false) 
              {
                 tooFar = true;
              }
              else
              {
              tooFar = false;
              }
              if (tooFar == false)
              {
                 gun.transform.RotateAroundLocal(Vector3.back, (rotateRate * Time.deltaTime) * -1f);
              }
              if (tooFar == true)
              {
                 rotate = false;
              }
         }
         else
         {
          rotate = false;
         }
       }
    }

still could use some help on this, i'm not sure if i should use the second vector as i never caught anything with trying it.

thanks!

more ▼

answered Jan 04 '12 at 03:25 AM

kievar1983 gravatar image

kievar1983
66 10 11 15

(comments are locked)
10|3000 characters needed characters left

The default rotation vector is interpreted as a quaternion, which is better for the maths but really hard to visualize. Try localEulerAngles instead, which is the XYZ rotation relative to the parent object:

Vector3 rot = gun.transform.rotation.localEulerAngles;
if  (Input.GetKey(KeyCode.S) && rot.z >= 350 || rot.z <= 20))
    rot.z -= rotateRate * Time.deltaTime;
gun.transform.rotation.localEulerAngles = rot;

You may also need to handle wrapping if the Z exceeds 360 or drops below 0, because it won't do it for you.

more ▼

answered Jan 03 '12 at 09:32 PM

Winterblood gravatar image

Winterblood
439 1 3 6

this kind of works, but it never stops spinning and the second i let go of s or W it just keeps spinning.

Jan 04 '12 at 02:07 AM kievar1983
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5275
x4374
x2249
x2175

asked: Jan 03 '12 at 08:41 PM

Seen: 2320 times

Last Updated: Jan 04 '12 at 03:25 AM