- Home /
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.
Your answer
Follow this Question
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