Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 bitbyte · Feb 13, 2012 at 08:57 AM · mousecontrolsmouse-lookspaceflight

Fluid and Constant Rotation for Space Sim?

So I made a small amateur-ish script for a fluid rotation of a "Gameobject", in this case a Spaceship. Instead of going the route of Quaternion(which frankly, the concept itself is quite complex for me to grasp wears a Dunce hat), I rather used rigidbody torque to achieve it. Here's the code for it.

 using UnityEngine;

 using System.Collections;



 public class MouseTurn : MonoBehaviour {

 

 public float pitchyawAmount = 0.5f;

    public float bankAmount = 0.5f;

 public float mouseSensitivity = 30f;

 public float angularDragAmt = 5f;    

 public float waitTime = 3f;
 

 private float horizontalaxeschange = 0f;

 private float verticalaxeschange = 0f;    

 private float angularvelocityamt = 0f;

 

 

 void Start ()

 {

     horizontalaxeschange = 0f;

     verticalaxeschange = 0f;

     angularDragAmt = 5f;        

     angularvelocityamt = 0f;

 }

 

 



 void FixedUpdate ()

 {

     Screen.lockCursor = true;

     

     if(Input.GetAxis("Horizontal") < 0){

         gameObject.rigidbody.AddRelativeTorque(0, 0, bankAmount);//transform.Rotate(0, 0, -bankAmount);

     }

         else if(Input.GetAxis("Horizontal") > 0){

         gameObject.rigidbody.AddRelativeTorque(0, 0, -bankAmount);//transform.Rotate(0, 0, bankAmount);

     }



         horizontalaxeschange = Input.GetAxis("Mouse X");// * pitchyawAmount;

         verticalaxeschange = Input.GetAxis("Mouse Y");// * pitchyawAmount;

     

     //print(angularvelocityamt);        

     angularvelocityamt = gameObject.rigidbody.angularVelocity.magnitude;



         gameObject.rigidbody.AddRelativeTorque(verticalaxeschange * pitchyawAmount, horizontalaxeschange * pitchyawAmount, 0);

     //gameObject.constantForce.torque = new Vector3(verticalaxeschange * pitchyawAmount, horizontalaxeschange * pitchyawAmount, 0);

     //transform.Rotate(verticalaxeschange * sensitivity, 0, -horizontalaxeschange * sensitivity);

     

     gameObject.rigidbody.angularDrag = angularDragAmt;

 }  

     

 void OnCollisionEnter(Collision SpaceShipHit){

     StartCoroutine("changeAngularDrag");

 }

 

 IEnumerator changeAngularDrag(){

     angularDragAmt = 10f;        

     yield return new WaitForSeconds(waitTime);        

     angularDragAmt = 5f;

 }    



 }


So my main objective here is to get a constant rotation for my gameobject when my mouse is at the edge of the screen(where my mouse should stay at all times). I did try regular rotation and also constantforce, which you can see in my code above, but they produced a very robotic/non-fluid rotation. Because of the angular drag, my gameobject comes to a halt if I stop moving my mouse. And if I decrease my angular drag, whenever I move my mouse multiple times, "AddRelativeTorque" shells out huge amount of torque, where it takes me more than a couple of mouse moves to make my gameobject stable again.

So where am I going wrong with this approach? Or is there an alternative to achieving a constant rotation with a fixed mouse cursor area?

P.S. I am using ONCollisionEnter() and subsequent coroutine to stabilize my gameobject after a collision, so that it won't go on a non stopping roll.

UPDATE: So after a through research or rigidbody and forces, I finally achieved what I was trying to do. I used ConstantForce.RelativeTorque under rigidbody to achieve a constant turn of my gameobject. If a mod sees this, they are welcome to close or delete this post if necessary. Thank You. Loving every minute of unity.

P.S For anyone looking for a X3/Black Prophecy type mouse movement, then here is a simpler version of the code I am using.

 void FixedUpdate ()

 {

     Screen.showCursor = false;

     

     float xvalue = Input.mousePosition.x;

     float yvalue = Input.mousePosition.y;

     

     int screenheight = Screen.height;

     int screenwidth = Screen.width;

     

     float xmidpoint = screenwidth/2;

     float ymidpoint = screenheight/2;



     //constantForce.relativeTorque = new Vector3(0f, (xvalue - xmidpoint) * 0.005f, 0f);



     constantForce.relativeTorque = new Vector3((yvalue - ymidpoint) * 0.01f, (xvalue - xmidpoint) * 0.005f, 0f);

     

     if(Input.GetAxis("Horizontal") < 0){

         //gameObject.rigidbody.AddRelativeTorque(0, 0, -bankAmount);//transform.Rotate(0, 0, -bankAmount);

         

         constantForce.relativeTorque = new Vector3(0f, 0f, 2f);

     }

     else if(Input.GetAxis("Horizontal") > 0){

         //gameObject.rigidbody.AddRelativeTorque(0, 0, bankAmount);//transform.Rotate(0, 0, bankAmount);

         

         constantForce.relativeTorque = new Vector3(0f, 0f, -2f);

     }

     

     

 }

Yeah took me a while to come up this this small/easy piece of code. hahaha.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Camera orbit around clicked position 0 Answers

How to pick up an object using left mouse button 2 Answers

How can I produce the typical FPS mouse steering effect? 2 Answers

Detect the position of the mouse press? 2 Answers

Inverted mouse controls 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