- Home /
Multiple Constant Force Inputs at once?
I have some modified code from a post on here, by a user named OoglyWoogly I believe, and I have added input for the mouse and keyboard, the keyboard as a backup. When moving the mouse along an axis, x or y, it only uses one input at a time, and I believe it's due to how constant force works. Would anyone be able to assist me, so that the object, being a spacecraft, will pitch/yaw at the same time depending on the mouse location?
private var previousMousePosition : Vector3;
var LThruster : ParticleSystem;
var RThruster : ParticleSystem;
var pilot : Rigidbody;
var invertPitch : boolean = true;
var invertYaw : boolean = false;
var sensitivity : float = 1.0;
function Start ()
{
//LThruster = GetComponentsInChildren.tag("LThruster");
LThruster.Stop(true);
//RThruster = transform.Find("RThruster");
RThruster.Stop(true);
rigidbody.AddForce(0,0,0);
}
function Update () {
// Did the user press fire?
//if (Input.GetButton ("Fire1"))
// BroadcastMessage("Fire");
if (Input.GetKeyDown("w"))
{
SetThrust(600);
LThruster.Play(true);
RThruster.Play(true);
}
if (Input.GetKeyUp("w"))
{
SetThrust(0);
LThruster.Stop(true);
RThruster.Stop(true);
}
if (Input.GetKeyDown("s"))
{
SetThrust(-500);
}
if (Input.GetKeyUp("s"))
{
SetThrust(0);
LThruster.Stop(true);
RThruster.Stop(true);
}
if (Input.GetKeyDown("q"))
{
SetRoll(400);
}
if (Input.GetKeyUp("q"))
{
SetRoll(0);
}
if (Input.GetKeyDown("e"))
{
SetRoll(-400);
}
if (Input.GetKeyUp("e"))
{
SetRoll(0);
}
if (Input.GetKeyDown("i"))
{
SetPitch(375);
}
if (Input.GetKeyUp("i"))
{
SetPitch(0);
}
if (Input.GetKeyDown("k"))
{
SetPitch(-375);
}
if (Input.GetKeyUp("k"))
{
SetPitch(0);
}
if (Input.GetKeyDown("j"))
{
SetYaw(355);
}
if (Input.GetKeyUp("j"))
{
SetYaw(0);
}
if (Input.GetKeyDown("l"))
{
SetYaw(-355);
}
if (Input.GetKeyUp("l"))
{
SetYaw(0);
}
if (Input.GetKeyDown("space"))
{
ThrustUp(300);
}
if (Input.GetKeyUp("space"))
{
ThrustUp(0);
}
if (Input.GetKeyDown(KeyCode.LeftControl))
{
ThrustUp(-300);
}
if (Input.GetKeyUp(KeyCode.LeftControl))
{
ThrustUp(0);
}
//Testing for mouse
if (Input.GetAxis("Mouse Y"))
{
SetPitch((Input.mousePosition.y - (Screen.height / 2)) * 2);
Debug.Log((Input.mousePosition.y - (Screen.height / 2)) * 2);//debug
}
if (Input.GetAxis("Mouse X"))
{
SetYaw((Input.mousePosition.x - (Screen.width / 2)) * 2);
Debug.Log((Input.mousePosition.x - (Screen.width / 2)) * 2);//debug
}
}
function SetThrust ( thrustForce : int)
{
pilot.constantForce.relativeForce = Vector3.forward * thrustForce * sensitivity;
}
function ThrustUp ( thrustForce : int)
{
pilot.constantForce.relativeForce = Vector3.up * thrustForce * sensitivity;
}
function SetRoll (rollForce : int)
{
pilot.constantForce.relativeTorque = Vector3(0,0,1) * rollForce * sensitivity;
}
function SetPitch (pitchForce : int)
{
if(invertPitch)
{
pilot.constantForce.relativeTorque = Vector3.right * pitchForce * sensitivity;
}
else
{
pilot.constantForce.relativeTorque = Vector3.left * pitchForce * sensitivity;
}
}
function SetYaw (yawForce : int)
{
if(invertYaw)
{
pilot.constantForce.relativeTorque = Vector3(0,1,0) * yawForce * sensitivity;
}
else
{
pilot.constantForce.relativeTorque = Vector3(0,-1,0) * yawForce * sensitivity;
}
}
Quick Edit: I've noticed that it doesn't happen only the mouse based axis, but instead, any multiple axis edit
Answer by kariwmklawm · Sep 01, 2014 at 12:28 PM
It's because both SetYaw and SetPitch are hard-setting the Vector3. So the Y axis of the mouse sets the pitch, then you set the X axis to the yaw, ending up with only one of the two inputs you want. If you want to still hard set the constant force values for individual axis, you'll want something like this:
function SetPitch(pitchForce : int)
{
if (invertPitch)
{
pilot.constantForce.relativeTorque =
Vector3(pitchForce * sensitivity,
pilot.constantForce.relativeTorque.y,
pilot.constantForce.relativeTorque.z);
}
else
{
pilot.constantForce.relativeTorque =
Vector3(-pitchForce * sensitivity,
pilot.constantForce.relativeTorque.y,
pilot.constantForce.relativeTorque.z);
}
}
function SetYaw(yawForce : int)
{
if (invertYaw)
{
pilot.constantForce.relativeTorque =
Vector3(pilot.constantForce.relativeTorque.x,
yawForce * sensitivity,
pilot.constantForce.relativeTorque.z);
}
else
{
pilot.constantForce.relativeTorque =
Vector3(pilot.constantForce.relativeTorque.x,
-yawForce * sensitivity,
pilot.constantForce.relativeTorque.z);
}
}
I'd like to upvote you but I can't. It seems to a work a hundred times better, thanks! I just started Javascript a bit ago, two weeks I'd say. Now I just need to work on multiple keyboard inputs. Same concept right?
Your answer
![](https://koobas.hobune.stream/wayback/20220613162002im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Input Axis Mouse ScrollWheel 1 Answer
Custom mouse cursor is laggy on low/mid hardware 0 Answers
Mouse input erratic 1 Answer
Raycast based on a Rect? 1 Answer
Mouse axis wont change an integar. 1 Answer