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