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 ThunderStrikeBlue · Jan 16, 2017 at 01:31 PM · c#controllercamera rotate

How to rotate a spacecraft using the mouse instead of butttons?

Hello,

I'm working on a space sim at the moment, and my current controls are quite difficult to use, as I have different input pairs for Roll, Pitch and Yaw. Is there any way that I could use the mouse's movement to alter the pitch and yaw, as Pitch is up/down, and Yaw is left/right. An example of this mechanic would be No Man's Sky, and I was wondering if anyone could help me implement it, with possibly some Camera delay too. My central control script is below.

 void Start()
 {
     rigidBody = GetComponent<Rigidbody>();
     OriginalDrag = rigidBody.drag;
     OriginalAngularDrag = rigidBody.angularDrag;

     for(int i = 0; i < transform.childCount; i++)
     {
         foreach(var componentsInChild in transform.GetChild(i).GetComponentsInChildren<WheelCollider>())
         {
             componentsInChild.motorTorque = 0.18f;
         }
     }
 }

 public void Move(float rollInput, float pitchInput, float yawInput, float throttleInput, float verticalInput, bool airBrakes)
 {
     this.RollInput = rollInput;
     this.PitchInput = pitchInput;
     this.YawInput = yawInput;
     this.ThrottleInput = throttleInput;
     this.AirBrakes = airBrakes;
     this.VerticalInput = verticalInput;
     
     Throttle = Input.GetAxis("Throttle");
     ClampInput();
     CalculateRollAndPitchAngles();
     //AutoLevel();
     CalculateForwardSpeed();
     ControlThrottle();
     CalculateDrag();
     CalculateLinearForces();
     CalculateTorque();
     if(Throttle < 0.1f)
     {
         Vector3 currentVelocity = rigidBody.velocity;
         Vector3 newVelocity = currentVelocity * Time.deltaTime;
         rigidBody.velocity = currentVelocity - newVelocity;
     }
     //TPS();
 }

 void ClampInput()
 {
     RollInput = Mathf.Clamp(RollInput, -1, 1);
     PitchInput = Mathf.Clamp(PitchInput, -1, 1);
     YawInput = Mathf.Clamp(YawInput, -1, 1);
     ThrottleInput = Mathf.Clamp(ThrottleInput, 0, 1);

 }

 void CalculateRollAndPitchAngles()
 {
     Vector3 flatForward = transform.forward;
     flatForward.y = 0;

     if(flatForward.sqrMagnitude > 0)
     {
         flatForward.Normalize();
         Vector3 localFlatForward = transform.InverseTransformDirection(flatForward);
         PitchAngle = Mathf.Atan2(localFlatForward.y, localFlatForward.z);

         Vector3 flatRight = Vector3.Cross(Vector3.up, flatForward);
         Vector3 localFlatRight = transform.InverseTransformDirection(flatRight);
         RollAngle = Mathf.Atan2(localFlatRight.y, localFlatRight.x);
     }
 }
 
 void AutoLevel()
 {
     BankedTurnAmount = Mathf.Sin(RollAngle);

     if(RollInput == 0f)
     {
         RollInput = RollAngle * AutoRollLevel;
     }

     if(PitchInput == 0f)
     {
         PitchInput = -PitchAngle * AutoPitchLevel;
         PitchInput -= Mathf.Abs(BankedTurnAmount * BankedTurnAmount * AutoTurnPitch);
     }
 }
 
 void CalculateForwardSpeed()
 {
     Vector3 localVelocity = transform.InverseTransformDirection(rigidBody.velocity);
     ForwardSpeed = Mathf.Max(0, localVelocity.z * 10);
 }   
 
 void ControlThrottle()
 {
     
     if (Immobilized)
     {
         ThrottleInput = -0.5f;
     }

     ThrottleInput = Mathf.Clamp01(Throttle + ThrottleInput * Time.deltaTime * ThrottleChangeSpeed);
     EnginePower = Throttle * MaxEnginePower;

     
 }

 void CalculateDrag()
 {
     float extraDrag = rigidBody.velocity.magnitude * DragIncreaseFactor;
     rigidBody.drag = (AirBrakes ? (OriginalDrag + extraDrag) * AirBreaksEffect : OriginalDrag + extraDrag);
     rigidBody.angularDrag = OriginalAngularDrag * ForwardSpeed / 1000 + OriginalAngularDrag;
 }

 void CalculateLinearForces()
 {
     Vector3 forces = Vector3.zero;
     forces += EnginePower * transform.forward;
     forces += VerticalInput * 100 * transform.up;

     rigidBody.AddForce(forces);

 }

 void CalculateTorque()
 {
     Vector3 torque = Vector3.zero;
     torque += PitchInput * PitchEffect * transform.right;
     torque += YawInput * 100 * transform.up;
     torque += RollInput * RollEffect * transform.forward;
     torque += BankedTurnAmount * BankedTurnEffect * transform.up;
     rigidBody.AddTorque(torque * AeroFactor);
 }

 public void Immobilize()
 {
     Immobilized = true;
 }

 public void Reset()
 {
     Immobilized = false;
 }

I'm pretty new to Unity and C#, and I'd really appreciate your help. I've tried to use the Camera section in the FirstPersonController asset script, but I can't wire it all up.

Comment
Add comment · Show 2
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
avatar image doublemax · Jan 16, 2017 at 01:34 PM 0
Share

I can't help directly, because i've never programmed anything like this myself, but as a gamer i'd like to make a personal remark: I think the flight controls in No $$anonymous$$an's Sky were "sub-optimal". Check out "Galaxy on Fire", i think the controls were pretty much perfect when played with mouse + keyboard.

avatar image ThunderStrikeBlue doublemax · Jan 18, 2017 at 06:13 PM 0
Share

Thanks for your help. Yeah, N$$anonymous$$S was a pretty clunky and unresponsive game when it came to spacecraft controls. I will go and check out Galaxy on Fire.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Creeper_Math · Jan 18, 2017 at 07:04 PM

Use the Input.GetAxis(string AxisName) for this... Here's the ones that would work here

 float YawChange = Input.GetAxis("Mouse X");
 float PitchChange = Input.GetAxis("Mouse Y");
 
 //("Yaw" and "Pitch" are public variables)
 
 Yaw += YawChange;
 Pitch += PitchChange;

Note that the input.getaxis gets the movement of the mouse, so you will have to use them as adding and subtracting like above

Comment
Add comment · Show 3 · Share
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
avatar image ThunderStrikeBlue · Jan 19, 2017 at 07:46 AM 0
Share

Thanks, bro! You made it look so simple... the FPS controller used $$anonymous$$athf.Epsilon and other $$anonymous$$athf stuff that I don't even know how to use! I will test it out when I get the chance.

avatar image ThunderStrikeBlue · Jan 19, 2017 at 07:48 AM 0
Share

Do you have any idea how i would delay camera rotation, so it was slightly slower to rotate into place?

avatar image Creeper_Math ThunderStrikeBlue · Jan 19, 2017 at 07:30 PM 0
Share

Here's the same script with the customized "sensitivity" (set to decimals for lower sensitivity)

 float YawChange = Input.GetAxis("$$anonymous$$ouse X");
 float $$anonymous$$chChange = Input.GetAxis("$$anonymous$$ouse Y");
 
 float YawSensitivity = 1.0f;
 float $$anonymous$$chSensitivity = 1.0f;
 
 //("Yaw" and "$$anonymous$$ch" are public variables)
 
 Yaw += YawChange * YawSensitivity;
 $$anonymous$$ch += $$anonymous$$chChange * $$anonymous$$chSensitivity;

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

8 People are following this question.

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

Related Questions

How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to create a Touch Pad to control camera rotation 0 Answers

3 person controler. rotate and turn 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