- Home /
Question by
jarratt51 · Mar 25, 2019 at 03:07 PM ·
scripting problemphysicsspacetorque
Kill Rotation of object using forces
Hi, I'm making a simple space flight simulator as a base for future games. The Player spaceship has realistic intertia based physics for space using Torque to induce turns in any axis similar to how RCS works in Kerbal Space Program. I want to create a void which (when a button is pressed) kills the rotation using the control torques similar to SAS in Kerbal Space Program but don't know how. Hard to explain really I'll attach my ShipController code.
Thanks :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShipController : MonoBehaviour
{
//Input Controls
private float m_Tilt;
private float m_Pitch;
private float m_Yaw;
private float m_MoveHorizontal;
private float m_MoveVertical;
private float m_MoveForBack;
private float m_Throttle;
private float m_ReverseEngineDirection;
//RCS Roll Pitch Yaw multiplyers
public float RollMultiplyer = 1;
public float PitchMultiplyer = 1;
public float YawMultiplyer = 1;
//RCS Thrust Size
public float ThrustMove = 1000;
public float ThrustRotate = 100;
//Main Engine Parameters
public float MainEngineThrust = 10000;
private List<int> MainEngineThrustList = new List<int>();
public int ThrottleSelect;
//parent rigidbody
public Rigidbody Ship;
//UIText
public Text T_Throttletext;
public Text T_Velocity;
public void SelectShip()
{
Ship = GetComponent<Rigidbody>();
}
private void InputControls()
{
m_Yaw = Input.GetAxis("Horizontal");
m_Pitch = Input.GetAxis("Vertical");
m_Tilt = Input.GetAxis("Tilt");
m_MoveHorizontal = Input.GetAxis("MoveHorizontal");
m_MoveVertical = Input.GetAxis("MoveVertical");
m_MoveForBack = Input.GetAxis("MoveForBack");
m_Throttle = Input.GetAxis("ThrottleControl");
m_ReverseEngineDirection = Input.GetAxis("ReverseEngineDirection");
}
private void MainEngineThrustSize()
{
for(int i = 0; i <= 100; i++)
{
MainEngineThrustList.Add(i);
}
}
public void MainEngine()
{
InputControls();
MainEngineThrustSize();
if (Input.GetKey("]"))
{
if(ThrottleSelect == 100)
{
ThrottleSelect = MainEngineThrustList[ThrottleSelect];
}
else
{
ThrottleSelect = MainEngineThrustList[ThrottleSelect + 1];
}
Debug.Log(ThrottleSelect);
}
else if (Input.GetKey("["))
{
if(ThrottleSelect == 0)
{
ThrottleSelect = MainEngineThrustList[ThrottleSelect];
}
else
{
ThrottleSelect = MainEngineThrustList[ThrottleSelect - 1];
}
Debug.Log(ThrottleSelect);
}
Ship.AddForce(transform.forward * ThrottleSelect * MainEngineThrust);
}
private void RCSMove()
{
InputControls();
SelectShip();
//Vertical Movement
Ship.AddForce(transform.up * m_MoveVertical * ThrustMove);
//ForwardBackMovement
Ship.AddForce(transform.forward * m_MoveForBack * ThrustMove);
//LeftRightMovement
Ship.AddForce(transform.right * m_MoveHorizontal * ThrustMove);
}
private void RCSRotate2()
{
InputControls();
SelectShip();
//Roll
Ship.AddRelativeTorque(Vector3.forward * RollMultiplyer * -m_Tilt);
//Pitch
Ship.AddRelativeTorque(Vector3.right * PitchMultiplyer * m_Pitch);
//Yaw
Ship.AddRelativeTorque(Vector3.up * YawMultiplyer * m_Yaw);
}
public void UIData()
{
T_Throttletext = GetComponent<Text>();
T_Velocity = GetComponent<Text>();
}
private void UIupdate()
{
MainEngine();
SelectShip();
T_Throttletext.text = "Throttle:" + ThrottleSelect.ToString();
T_Velocity.text = "Velocity:" + Ship.velocity.magnitude.ToString("f0");
}
// Update is called once per frame
private void Start()
{
MainEngineThrustSize();
ThrottleSelect = MainEngineThrustList[0];
}
void FixedUpdate()
{
MainEngine();
RCSMove();
RCSRotate2();
UIupdate();
}
}
Comment
well, if you are using unity physics engine, yoou could use the angular drag in the rigidbody, try increasing it and check if that fixed a bit the problem
Your answer
![](https://koobas.hobune.stream/wayback/20220612194245im_/https://answers.unity.com/themes/thub/images/avi.jpg)