Search Unity

Swimmin'

Discussion in 'Editor & General Support' started by nickavv, Sep 21, 2006.

  1. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    This is probably the most random problem I've ever had, but here goes nothin'. I'm making a game about Sgid (don't ask) and I wrote a script to make him float around. The script looks like this:

    Code (csharp):
    1. function Update () {
    2.     if (Input.GetButton("right")) {
    3.         transform.Rotate (0, 8, 0);
    4.     }
    5.     if (Input.GetButton("left")) {
    6.         transform.Rotate (0, -8, 0);
    7.     }
    8.     if (Input.GetButton("up")) {
    9.         rigidbody.AddRelativeForce (Vector3.forward * 15);
    10.     }
    11.     if (Input.GetButton("down")) {
    12.         rigidbody.AddRelativeForce (Vector3.back * 15);
    13.     }
    14.     if (Input.GetButton("FloatUp")) {
    15.         rigidbody.AddRelativeForce (Vector3.up * 15);
    16.     }
    17.     if (Input.GetButton("FloatDown")) {
    18.         rigidbody.AddRelativeForce (Vector3.down * 15);
    19.     }
    20. }
    But when I press the button for FloatUp he floats up while rotating counter-clockwise. Why is he rotating? :(
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Maybe some of the keyboard mapping are mapped to the same key.

    Just some suggestions for the script. It is a lot easier, faster and works with joysticks to use axes instead of if's.

    Code (csharp):
    1.  
    2.  if (Input.GetButton("right")) {
    3.       transform.Rotate (0, 8, 0);
    4.    }
    5.    if (Input.GetButton("left")) {
    6.       transform.Rotate (0, -8, 0);
    7.    }
    8.  
    becomes this:
    Code (csharp):
    1.  
    2.  transform.Rotate (0, Input.GetAxis("Horizontal") * 8, 0);
    3.  
    secondly you want to make it frame rate independent by multiplying with delta time:
    Code (csharp):
    1.  
    2.  transform.Rotate (0, Input.GetAxis("Horizontal") * 200 * Time.deltaTime, 0);
    3.  
    This makes it rotate exactly 200 degrees per second, independent of frame rate.

    You can apply the same GetAxis instead of if (button ) technique to the addforce calls.
     
  3. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    They definately aren't mapped to the same button. Completely seperate. :/