Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Spacecraft script WIP seeking advice

Discussion in 'Scripting' started by CrossGuard-Games, Nov 28, 2010.

  1. CrossGuard-Games

    CrossGuard-Games

    Joined:
    Jan 12, 2010
    Posts:
    226
    Okay, after allot of work (from a beginner mind you) I have a crude, semi-working spaceship control script, with the help of the forums as always, and I referenced Tiles airplane script quiet a bit, (Good stuff man). I still have a couple major issues, the most obvious at the moment is that when flying down and turning left or right the ship doesn't really turn left or right, I'm not exactly sure whats causing it, you can be sure I'll be digging around in there trying to figure it out, but if anyone wants to drop me a tip or two I'd love it, Here's the script, feel free to try it out, you should be able to put it onto any object and fly!



    Code (csharp):
    1. function Update () {
    2.        
    3. transform.Rotate(Input.GetAxis("Vertical") * Time.deltaTime * 100,0,0);
    4. transform.Rotate(0,Input.GetAxis("Horizontal") * Time.deltaTime * 100,0,Space.World);
    5. transform.Rotate(0,0,-Input.GetAxis("Horizontal") * Time.deltaTime * 100);
    6.  
    7. //limit tilt
    8. if (transform.eulerAngles.x >45  transform.eulerAngles.x<180)
    9. transform.eulerAngles.x = 45;
    10. else if (transform.eulerAngles.x<315  
    11. transform.eulerAngles.x>180) transform.eulerAngles.x = 315;
    12.  
    13. if (transform.eulerAngles.z >45  transform.eulerAngles.z<180)
    14. transform.eulerAngles.z = 45;
    15. else if (transform.eulerAngles.z<315  
    16. transform.eulerAngles.z>180) transform.eulerAngles.z = 315;
    17.  
    18.  
    19.  
    20.  
    21. //return to 0 if nothing is pressed
    22. if((transform.eulerAngles.z>0)  (transform.eulerAngles.z>180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,100 * Time.deltaTime);
    23. if((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,0);
    24. if((transform.eulerAngles.z>0)  (transform.eulerAngles.z<180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,-100 * Time.deltaTime);
    25. if(((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal")))) transform.Rotate(0,0,0);
    26.  
    27. if((transform.eulerAngles.x>0)  (transform.eulerAngles.x>180)  (!Input.GetAxis("Vertical"))) transform.Rotate(100 * Time.deltaTime,0,0);
    28. if((transform.eulerAngles.x == 0)  (!Input.GetAxis("Vertical"))) transform.Rotate(0,0,0);
    29. if((transform.eulerAngles.x>0)  (transform.eulerAngles.x<180)  (!Input.GetAxis("Vertical"))) transform.Rotate(-100 * Time.deltaTime,0,0);
    30. if(((transform.eulerAngles.x == 0)  (!Input.GetAxis("Vertical")))) transform.Rotate(0,0,0);
    31.  
    32. //always forward
    33. transform.Translate(0,0,20 * Time.deltaTime);
    34.  
    35.  
    36. }
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Not a bad script, you are correct, the way you have it, you are telling it to "adjust for the x rotation, then erase that, then adjust for the y rotation, then erase that then adjust for the z rotation", thus erasing left and right when you complete it...

    You should of course combine all three to get your new rotation. See the script below.

    I tinkered with the code a bit I just like putting variables so that you can adjust things like speed and rotation.

    Code (csharp):
    1.  
    2. var rotationRate=200;
    3. var returnRotationRate=50;
    4. var speed=50;
    5.  
    6. function Update () {
    7.  
    8. //transform.Rotate(Input.GetAxis("Vertical") * Time.deltaTime * rotationRate,0,0);
    9. //transform.Rotate(0,Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate,0,Space.World);
    10. //transform.Rotate(0,0,-Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate);
    11.  
    12. transform.Rotate(Input.GetAxis("Vertical") * Time.deltaTime * rotationRate,
    13.     Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate,
    14.     -Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate);
    15.  
    16. //limit tilt
    17. if (transform.eulerAngles.x >45  transform.eulerAngles.x<180)
    18. transform.eulerAngles.x = 45;
    19. else if (transform.eulerAngles.x<315  
    20. transform.eulerAngles.x>180) transform.eulerAngles.x = 315;
    21.  
    22. if (transform.eulerAngles.z >45  transform.eulerAngles.z<180)
    23. transform.eulerAngles.z = 45;
    24. else if (transform.eulerAngles.z<315  
    25. transform.eulerAngles.z>180) transform.eulerAngles.z = 315;
    26.  
    27.  
    28.  
    29.  
    30. //return to 0 if nothing is pressed
    31. if((transform.eulerAngles.z>0)  (transform.eulerAngles.z>180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,returnRotationRate * Time.deltaTime);
    32. if((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,0);
    33. if((transform.eulerAngles.z>0)  (transform.eulerAngles.z<180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,-returnRotationRate * Time.deltaTime);
    34. if(((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal")))) transform.Rotate(0,0,0);
    35.  
    36. if((transform.eulerAngles.x>0)  (transform.eulerAngles.x>180)  (!Input.GetAxis("Vertical"))) transform.Rotate(returnRotationRate * Time.deltaTime,0,0);
    37. if((transform.eulerAngles.x == 0)  (!Input.GetAxis("Vertical"))) transform.Rotate(0,0,0);
    38. if((transform.eulerAngles.x>0)  (transform.eulerAngles.x<180)  (!Input.GetAxis("Vertical"))) transform.Rotate(-returnRotationRate * Time.deltaTime,0,0);
    39. if(((transform.eulerAngles.x == 0)  (!Input.GetAxis("Vertical")))) transform.Rotate(0,0,0);
    40.  
    41. //always forward
    42. transform.Translate(0,0,speed * Time.deltaTime);
    43.  
    44.  
    45. }
    46.  
     
  3. CrossGuard-Games

    CrossGuard-Games

    Joined:
    Jan 12, 2010
    Posts:
    226
    Ahh, I see, Thanks. I still gotta make it so that it doesn't move down when you go side to side. Is there possibly a way to make only the y rotation world space? Hmm, anyone know the best way to keep it going only side to side but still rotate on the z axis when left or right is pressed?
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I think the simplest way is to create a second object. That object would be the parent object of the one you are working on. That object would only receive the x and y rotations (turning the craft) where the ship would receive the z rotation giving the appearance of banking.

    You will also need to adjust the transform orientations throughout the code to reflect the parenting.

    I just hacked this code together without testing, there could be a couple of .parent's that I missed.
    Code (csharp):
    1.  
    2. var rotationRate=200;
    3. var returnRotationRate=50;
    4. var speed=50;
    5.  
    6. function Update () {
    7.  
    8. //transform.Rotate(Input.GetAxis("Vertical") * Time.deltaTime * rotationRate,0,0);
    9. //transform.Rotate(0,Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate,0,Space.World);
    10. //transform.Rotate(0,0,-Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate);
    11.  
    12.  
    13. transform.parent.Rotate(Input.GetAxis("Vertical") * Time.deltaTime * rotationRate,
    14.     Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate,
    15.     0);
    16.  
    17. transform.Rotate(0,
    18.     Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate,
    19.     -Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate);
    20.  
    21. //limit tilt
    22. if (transform.parent.eulerAngles.x >45  transform.parent.eulerAngles.x<180)
    23. transform.parent.eulerAngles.x = 45;
    24. else if (transform.parent.eulerAngles.x<315  
    25. transform.parent.eulerAngles.x>180) transform.parent.eulerAngles.x = 315;
    26.  
    27. if (transform.eulerAngles.z >45  transform.eulerAngles.z<180)
    28. transform.eulerAngles.z = 45;
    29. else if (transform.eulerAngles.z<315  
    30. transform.eulerAngles.z>180) transform.eulerAngles.z = 315;
    31.  
    32.  
    33.  
    34.  
    35. //return to 0 if nothing is pressed
    36. if((transform.eulerAngles.z>0)  (transform.eulerAngles.z>180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,returnRotationRate * Time.deltaTime);
    37. if((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,0);
    38. if((transform.eulerAngles.z>0)  (transform.eulerAngles.z<180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,-returnRotationRate * Time.deltaTime);
    39. if(((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal")))) transform.Rotate(0,0,0);
    40.  
    41. if((transform.parent.eulerAngles.x>0)  (transform.parent.eulerAngles.x>180)  (!Input.GetAxis("Vertical"))) transform.Rotate(returnRotationRate * Time.deltaTime,0,0);
    42. if((transform.parent.eulerAngles.x == 0)  (!Input.GetAxis("Vertical"))) transform.parent.Rotate(0,0,0);
    43. if((transform.parent.eulerAngles.x>0)  (transform.parent.eulerAngles.x<180)  (!Input.GetAxis("Vertical"))) transform.parent.Rotate(-returnRotationRate * Time.deltaTime,0,0);
    44. if(((transform.parent.eulerAngles.x == 0)  (!Input.GetAxis("Vertical")))) transform.parent.Rotate(0,0,0);
    45.  
    46. //always forward
    47. transform.parent.Translate(0,0,speed * Time.deltaTime);
    48.  
    49.  
    50. }
    51.  
     
  5. foxter888

    foxter888

    Joined:
    May 3, 2010
    Posts:
    530
    your code seems to be more good if your spaceship has a character controller collider now my question would be is your ship a rigidbody? and if it is have you tried making your code using the rigidbody functions? such as AddTorque or AddForce it would make the code a lot smaller thus having physics for your ship.now for more about it you should go to the scripting reference for it and if for example your ship rotates in a way it does not supposed to use the configurable joint to lock those rotations and that is actually a component and not a code by the way. this is just an idea to try if you are getting problems sometimes there are situations in which you might have to end up using a diffeent aproach in your project.
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    There was no rigid body attached to the ship for that code, it had funky problems when I had attached it. I am sure the rigid body functions would do fine. I am working on a physics airplane.
     
  7. CrossGuard-Games

    CrossGuard-Games

    Joined:
    Jan 12, 2010
    Posts:
    226
    Hey MisterB, checked it out a little, I got some funky stuff going on with the parent stuff but so I ended up applying a separate script to the parent object and it worked, I think I just missed some stuff though. It's late now, so I'll be giving it another shot next chance I get. I got it nearly work fine, the only little thing is if your flying down, and you press left/right, and turn all the way in that direction, then let go of the left button and press the right button you will stop turning left entirely and continue to dive down, but you wont turn right, you have to let the ship level out a little before you can continue to dive down and turn right, which would be a pretty frustrating situation I think. Like I said I wanna try to get it all into one script, if I do that and I'm still having trouble I post it on here.

    Foxter, no rigid body on here, I messed around with addtorque though, it looked pretty cool, I be sure to check em out in the future, thanks for the input.
     
  8. CrossGuard-Games

    CrossGuard-Games

    Joined:
    Jan 12, 2010
    Posts:
    226
    Okay here's what I got, it all works great except one little thing, if you go up or down AND left or right the parent eventually rotates and starts acting weird, however if you only go up or down, or only go left or right, then it's stays fine. Here's the script

    Code (csharp):
    1. var returnRotationRate = 50;
    2. function Update () {
    3.  
    4. transform.Rotate(0, 0,-Input.GetAxis("Horizontal") * Time.deltaTime * 100);
    5.  
    6. //but dont rotate too much
    7. if (transform.eulerAngles.z >45  transform.eulerAngles.z<180)
    8. transform.eulerAngles.z = 45;
    9. else if (transform.eulerAngles.z<315  
    10. transform.eulerAngles.z>180) transform.eulerAngles.z = 315;
    11.  
    12.  
    13. //stop rotation if nothing is pressed
    14.     if((transform.eulerAngles.z>0)  (transform.eulerAngles.z>180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,returnRotationRate * Time.deltaTime);
    15. if((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,0);
    16. if((transform.eulerAngles.z>0)  (transform.eulerAngles.z<180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,-returnRotationRate * Time.deltaTime);
    17. if(((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal")))) transform.Rotate(0,0,0);
    18.  
    19.  
    20.  
    21. //parent code
    22. transform.parent.Rotate(Input.GetAxis("Vertical") * Time.deltaTime * 100,0,0);
    23. transform.parent.Rotate(0, Input.GetAxis("Horizontal") * Time.deltaTime * 100,0,Space.World);
    24.  
    25.  
    26. //limit parent rotation
    27.  
    28.  
    29. if (transform.parent.eulerAngles.x >45  transform.parent.eulerAngles.x<180)
    30. transform.parent.eulerAngles.x = 45;
    31. else if (transform.parent.eulerAngles.x<315  
    32. transform.parent.eulerAngles.x>180) transform.parent.eulerAngles.x = 315;
    33.  
    34.     if((transform.parent.eulerAngles.x>0)  (transform.parent.eulerAngles.x>180)  (!Input.GetAxis("Vertical"))) transform.parent.Rotate(returnRotationRate * Time.deltaTime,0,0);
    35. if((transform.parent.eulerAngles.x == 0)  (!Input.GetAxis("Vertical"))) transform.parent.Rotate(0,0,0);
    36. if((transform.parent.eulerAngles.x>0)  (transform.parent.eulerAngles.x<180)  (!Input.GetAxis("Vertical"))) transform.parent.Rotate(-returnRotationRate * Time.deltaTime,0,0);
    37. if(((transform.parent.eulerAngles.x == 0)  (!Input.GetAxis("Vertical")))) transform.parent.Rotate(0,0,0);
    38.  
    39. transform.parent.Translate(0,0, Time.deltaTime * 50);
    40.  
    41.  
    42. }
    it seams like it has to do with the

    Code (csharp):
    1. if (transform.parent.eulerAngles.x >45  transform.parent.eulerAngles.x<180)
    2. transform.parent.eulerAngles.x = 45;
    3. else if (transform.parent.eulerAngles.x<315  
    4. transform.parent.eulerAngles.x>180) transform.parent.eulerAngles.x = 315;
    But that bit of code was suggested to me, so I don't understand it fully. Anyone got any advice I feel like I'm very close with this and I would love some advice, thanks.
     
  9. CrossGuard-Games

    CrossGuard-Games

    Joined:
    Jan 12, 2010
    Posts:
    226
  10. gooongalooo

    gooongalooo

    Joined:
    May 6, 2010
    Posts:
    140
    could you show an example of what you are trying to do? at the moment...for me it just looks like a body rotates some degrees on it´s own axis ... ;) should it behave like a spaceship or more like an airplane?
     
  11. CrossGuard-Games

    CrossGuard-Games

    Joined:
    Jan 12, 2010
    Posts:
    226
    The ship itself is parented to a cube, and the ship rotates on the x and y axis around the parent cube so that it can rotate on the z axis to simulate banking, I believe it's called. It is supposed to be a Starfox style space ship, so it should behave like a spacecraft. And it always has a forward momentum, which is one of the last parts of the script.
    Does that help, or should I figure out how to upload the file? :p
     
  12. CrossGuard-Games

    CrossGuard-Games

    Joined:
    Jan 12, 2010
    Posts:
    226
    Okay this is embarrassing, I want to upload the build but I never have before, I tried attaching as an attachment but that didn't work, could anyone direct me in the proper manner for this.