rotate my ship or rolling it to left an right

Hi i want that my ship rolles when i press "q" to left and on "e" to the right. Every rotation i tried didnt work. Here is an example of what i tried.

var speed = 200;
var acc = 60;
var acc2 = 160;
var mom = 0;
var momb = 0;
var movleft = 0;
var movright = 0;
var rotleft = 0;
var rotright = 0;
var besch = 1;
var beschr = 20;
var hyper = 2000;

var smooth = 2.0;
var tiltAngle = 30.0;

var rotateSpeed = 20.0;

function Update () {

if (mom < 0){
    mom = 0;
}
if (Input.GetKey ("up") || Input.GetKey ("w")) {
    if (mom <= speed){
        mom = mom + besch;
        if (mom > speed){
            mom = speed;
        }
    }
} else {
    if (mom >= 0){
        mom = mom - besch;
    } else {
        mom = 0;
    }
}
if (Input.GetKey ("down") || Input.GetKey ("s")) {

        mom = mom - besch;
        if (mom < 0){
            mom = 0;
        }
}
if (Input.GetKey ("left") || Input.GetKey ("a")) {
    if (movleft <= acc){
        movleft = movleft + besch;
        if (movleft >= acc){
            movleft = acc;
        }
    }
} else {
    if (movleft > 0){
        movleft = movleft - besch;
    }
}
if (Input.GetKey ("right") || Input.GetKey ("d")) {
    if (movright <= acc){
        movright = movright + besch;
        if (movright >= acc){
            movright = acc;
        }
    }
} else {
    if (movright > 0){
        movright = movright - besch;
    }
}

if (Input.GetKey ("e")) {
    if (rotright <= acc2){
        rotright = rotright + beschr;
        if (rotright >= acc2){
            rotright = acc2;
        }
    }
} else {
    if (rotright > 0){
        rotright = rotright - beschr;
    }
}

if (Input.GetKey ("q")) {
    if (rotleft <= acc2){
        rotleft = rotleft + beschr;

    }
} else {
    if (rotleft > 0){
        rotleft = rotleft - beschr;
    }
}

if (Input.GetKey ("j")) {

        speed = hyper;
        besch = 10;
}
if (mom < 0){
            mom = 0;
        }
transform.Translate(0, 0, Time.deltaTime*mom);
transform.Translate(0, 0, -Time.deltaTime*momb);
transform.Translate(-Time.deltaTime*movleft, 0,0);
transform.Translate(Time.deltaTime*movright, 0, 0);

//transform.Rotate (0, 0, -Time.deltaTime*rotright);
//transform.Rotate (0, 0, Time.deltaTime*rotleft);
//transform.Rotate(Vector3(0, 0, 1), Time.deltaTime*rotleft);
//transform.Rotate(Vector3(0, 0, 1), -Time.deltaTime*rotright);

//var tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
var tiltAroundZ = Input.GetKey ("q") * 30;
//var target = Quaternion.Euler (0, tiltAroundX, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, target,Time.deltaTime * smooth);

}

Are you trying to do a complete barrel roll? Or just a tilt?

3 Answers

3

You could try animating it in your modeling application... That would be the easiest thing to do. Just animate the ship rolling and then tell unity to play the animation when you press the button and while playing the animation move the ship over to the side. Hope this helps!! Have fun making your ship game!

Why are you multiplying by negative delta time? What are the values of rotleft/rotright and tiltAngle? Why does try3 not use tiltAroundZ anywhere? (Actually that'll probably solve your problem, add tiltAroundZ as the z value on your target rotation.)

Have you considered using something more off the shelf, like perhaps this script: http://www.unifycommunity.com/wiki/index.php?title=ShipControls

Actually I found that for my ship game those default ship controls failed miserably. I wouldn't recommend them!

I profess that the script doesn't make a lot of sense to me. When you say it didn't work, was that not at all, or different to expected? I can see some bits of code here that will give rotation behaviour, but can't quite follow it. For instance the Quaternion.Slerp() seems to call on "target" but I don't see that declared anywhere.

Most of the problem with rotations of this kind are down to rotating the wrong axis, or by a value that is too large or small. A ship should probably roll on its z axis (in 3D). If it is rotated by a value you are adding/subtracting to each frame based on key input, remember to decay it (movright *= 0.9; each frame for example) or you'll just keep spinning.

For roll, I would use one value like so (heavily simplified):

var rollSpeed = 5.0;
var rollAmount = 0.0;

function Update() {

  rollAmount *= 0.9;

  if (Input.GetKey("q")) {
    rollAmount += rollSpeed ;
  } else if (Input.GetKey("e")) {
    rollAmount -= rollSpeed ;
  }

  transform.Rotate(0, 0, rollAmount*Time.deltaTime);

}

Try that on a cube or something first, then you could probably copy the behaviour onto your ship as appropriate. Let me know if the behaviour you're getting persists. Incidentally, I would advise against just taking a script to solve your problem.

Sounds like you are setting the angle rather than rotating by the value. This script shouldn't have any boundries like that, I will analyse it further when at home

Nope... it goes all the way around for me. Try attaching the above script to a cube in a blank project, you will see what I mean. Perhaps another value is being set rather than incremented? (ie pitch or yaw)