- Home /
RigidBody.AddRelativeTorque // Input.GetButtonDown issues
Hello there, I've recently begun delving into Unity and C#, and have attempted to make a simple 3D space game, where you fly around. Currently, everything works as intended, and I am using rigidbody.AddRelativeTorque to change the pitch and roll of the object. I have a few issues.
Firstly, the controls seem rather clunky. I've experimented with Input.GetKey, .GetKeyDown, .GetButton .GetButtonDown, etc, in an attempt to make it so that the actions only happen whilst the key is actually being physically held down, and NOT happening once it is released. Currently if the key is tapped, it will continue to move until the opposite key is tapped, upon which it will slow down (but not stop)
The second issue that I am having is that this is happening too quickly. The changes occur much quicker than desiered, and I have played around with the "rollspeed" value in an attempt to change this, with no luck.
I've not been working with C# and Unity for very long, so please forgive my inexperience. My code is as follows.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
// Use this for initialization
public float rollspeed = 0.01f;
void Update()
{
///////////////////////////ROLL LEFT///////////////////////
if (Input.GetButtonDown("Roll") && Input.GetAxis("Roll") < 0)
{
Debug.Log("Roll Left"); //Display it is picking up that you've pressed the button
rigidbody.AddRelativeTorque(0,0, rollspeed); //MOVES CORRECTLY but too fast
}
///////////////////////////ROLL RIGHT///////////////////////
if (Input.GetButtonDown("Roll") && Input.GetAxis("Roll") > 0)
{
Debug.Log("Roll Right");
rigidbody.AddRelativeTorque(0, 0, -rollspeed);
}
///////////////////////////PITCH UP///////////////////////
if (Input.GetButton("Pitch") && Input.GetAxis("Pitch") > 0 )
{
Debug.Log("Pitch Up");
rigidbody.AddRelativeTorque(rollspeed, 0, 0);
}
///////////////////////////PITCH DOWN///////////////////////
if (Input.GetButton("Pitch") && Input.GetAxis("Pitch") < 0 )
{
Debug.Log("Pitch Down");
rigidbody.AddRelativeTorque(-rollspeed, 0, 0);
}
}
}
I would like to know what I need to change in order to;
-Limit the speed these changes take placed (preferably based upon a variable)
-Make these changes ONLY when the button is held
If someone could assist me in working around my problem, I would greatly appreciate it. (This is the first time I've used Unity Answers. Apologies for any incorrect formatting)
Regards,
Ben
Answer by Bejasc · Dec 19, 2012 at 10:37 PM
After fiddling around I have stumbled upon a solution. The new code is now
If someone could confirm that this new way is the most efficient or reliable way to do what I have asked it to do, or if there is another, better way, please let me know
///////////////////////////ROLL LEFT///////////////////////
if (Input.GetKey(KeyCode.A))
{
Debug.Log("A key is Down");
rigidbody.transform.Rotate(0, 0, rollspeed*2);
}
///////////////////////////ROLL RIGHT///////////////////////
if (Input.GetKey(KeyCode.D))
{
Debug.Log("D key is Down");
rigidbody.transform.Rotate(0, 0, -rollspeed*2);
}
///////////////////////////PITCH UP///////////////////////
if (Input.GetKey(KeyCode.S))
{
Debug.Log("Pitch Up");
rigidbody.transform.Rotate(-rollspeed, 0, 0);
}
///////////////////////////PITCH DOWN///////////////////////
if (Input.GetKey(KeyCode.W))
{
Debug.Log("Pitch Down");
rigidbody.transform.Rotate(rollspeed, 0, 0);
}
Answer by Banksy · Aug 15, 2013 at 09:46 AM
I believe void Update() is not efficient when using physics..
void FixedUpdate() is much better.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Swipe and Hold to move character . 1 Answer
Missed key input 1 Answer
Hover Over Input Field Before Inputting? 2 Answers