Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fadawar · Oct 13, 2015 at 10:57 PM · c#movementcontrollerspaceship

Spaceship Control Problems

I am using the script from here, in order to control my space ship but for some reason I can't figure out why I cant turn the ship all the way around. I took a screen grab that shows my problem and that can be viewed here. Also I have slightly edited the code for my purposes, so I have included my actual code as seen in monodelevop. What is causing this and how might I fix this problem? Thank You!!

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour
 {
     //speed stuff
     float speed;
     public int cruiseSpeed;
     float deltaSpeed;//(speed - cruisespeed)
     public int minSpeed;
     public int maxSpeed;
     float accel, decel;
     
     //turning stuff
     Vector3 angVel;
     Vector3 shipRot;
     public int sensitivity;
 
 
     
     public Vector3 cameraOffset; //I use (0,1,-3)
     
     void Start()
     {
         speed = cruiseSpeed;
     }
     
     void FixedUpdate()
     {
         
         //ANGULAR DYNAMICS//
         
         shipRot = transform.localEulerAngles; //make sure you're getting the right child (the ship).  I don't know how they're numbered in general.
         
         //since angles are only stored (0,360), convert to +- 180
         if (shipRot.x > 180) shipRot.x -= 360;
         if (shipRot.y > 180) shipRot.y -= 360;
         if (shipRot.z > 180) shipRot.z -= 360;
         
         //vertical stick adds to the pitch velocity
         //         (*************************** this *******************************) is a nice way to get the square without losing the sign of the value
         angVel.x += Input.GetAxis("Vertical") * Mathf.Abs(Input.GetAxis("Vertical")) * sensitivity * Time.fixedDeltaTime;
         
         //horizontal stick adds to the roll and yaw velocity... also thanks to the .5 you can't turn as fast/far sideways as you can pull up/down
         float turn = Input.GetAxis("Horizontal") * Mathf.Abs(Input.GetAxis("Horizontal")) * sensitivity * Time.fixedDeltaTime;
         angVel.y += turn * .5f;
         angVel.z -= turn * .5f;
         
 
         //shoulder buttons add to the roll and yaw.  No deltatime here for a quick response
         //comment out the .y parts if you don't want to turn when you hit them
         if (Input.GetKey(KeyCode.Joystick1Button4) || Input.GetKey(KeyCode.I))
         {
             angVel.y -= 20;
             angVel.z += 50;
             speed -= 5 * Time.fixedDeltaTime;
         }
         
         if (Input.GetKey(KeyCode.Joystick1Button5) || Input.GetKey(KeyCode.O))
         {
             angVel.y += 20;
             angVel.z -= 50;
             speed -= 5 * Time.fixedDeltaTime;
         }
         
         
         //your angular velocity is higher when going slower, and vice versa.  There probably exists a better function for this.
         angVel /= 1 + deltaSpeed * .001f;
         
         //this is what limits your angular velocity.  Basically hard limits it at some value due to the square magnitude, you can
         //tweak where that value is based on the coefficient
         angVel -= angVel.normalized * angVel.sqrMagnitude * .05f * Time.fixedDeltaTime;
         
         
         //and finally rotate.  
         transform.Rotate(angVel * Time.fixedDeltaTime);
         
         //this limits your rotation, as well as gradually realigns you.  It's a little convoluted, but it's
         //got the same square magnitude functionality as the angular velocity, plus a constant since x^2
         //is very small when x is small.  Also realigns faster based on speed.  feel free to tweak
         transform.Rotate(-shipRot.normalized * .015f * (shipRot.sqrMagnitude + 500) * (1 + speed / maxSpeed) * Time.fixedDeltaTime);
         
         
         //LINEAR DYNAMICS//
         
         deltaSpeed = speed - cruiseSpeed;
         
         //This, I think, is a nice way of limiting your speed.  Your acceleration goes to zero as you approach the min/max speeds, and you initially
         //brake and accelerate a lot faster.  Could potentially do the same thing with the angular stuff.
         decel = speed - minSpeed;
         accel = maxSpeed - speed;
         
         //simple accelerations
         if (Input.GetKey(KeyCode.Joystick1Button1) || Input.GetKey(KeyCode.LeftShift))
             speed += accel * Time.fixedDeltaTime;
         else if (Input.GetKey(KeyCode.Joystick1Button0) || Input.GetKey(KeyCode.Space))
             speed -= decel * Time.fixedDeltaTime;
         
         //if not accelerating or decelerating, tend toward cruise, using a similar principle to the accelerations above
         //(added clamping since it's more of a gradual slowdown/speedup)
         else if (Mathf.Abs(deltaSpeed) > .1f)
             speed -= Mathf.Clamp(deltaSpeed * Mathf.Abs(deltaSpeed), -30, 100) * Time.fixedDeltaTime;
         
         
         //moves camera (make sure you're GetChild()ing the camera's index)
         //I don't mind directly connecting this to the speed of the ship, because that always changes smoothly
 //        transform.GetChild(0).localPosition = cameraOffset + new Vector3(0, 0, -deltaSpeed * .02f);
         
         
         float sqrOffset = transform.position.sqrMagnitude;
         Vector3 offsetDir = transform.position.normalized;
         
         
         //this takes care of realigning after collisions, where the ship gets displaced due to its rigidbody.
         //I'm pretty sure this is the best way to do it (have the ship and the rig move toward their mutual center)
         //transform.Translate(-offsetDir * sqrOffset * 20 * Time.fixedDeltaTime);
         //(**************** this ***************) is what actually makes the whole ship move through the world!
         transform.Translate((transform.forward * speed) * Time.fixedDeltaTime, Space.World);
 
         //comment this out for starfox, remove the x and z components for shadows of the empire, and leave the whole thing for free roam
 //        transform.Rotate(shipRot.x * Time.fixedDeltaTime, (shipRot.y * Mathf.Abs(shipRot.y) * .02f) * Time.fixedDeltaTime, shipRot.z * Time.fixedDeltaTime);
     }
     
     void Update()
     {
     }
 }

 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Two Horizontal Movement Axes Behaving Differently 0 Answers

Three Lane Character Movement, Help Please? 0 Answers

Player Movement 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges