Get a constant Speed
Hi i have a object that go forward and jump in the air and my x axis is slowing when i set flap at true or when i get in contact with another object. Is there possible to get a constant x speed so my GameObject won't slow down?
Here is my code:
Vector3 velocity = Vector3.zero;
public Vector3 flapVelocity;
public float flapSpeed = 1.2f;
public float ForwardSpeed = 1f;
bool flap = false;
bool reverse = false;
bool changeGravity = false;
// Use this for initialization
void Start()
{
}
//Graphiques et mises à jour d'inputs
void Update()
{
if (Input.GetKeyDown("space") || Input.GetMouseButtonDown(0))
{
flap = true;
}
else if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetMouseButtonDown(0))
{
if (reverse == false)
{
reverse = true;
}
else
{
reverse = false;
}
changeGravity = true;
}
}
// Mises à jour des moteurs physiques
void FixedUpdate()
{
Rigidbody2D saut = GetComponent<Rigidbody2D>();
if (changeGravity == true)
{
if (reverse == true)
{
GetComponent<Rigidbody2D>().gravityScale = -1.8f;
changeGravity = false;
}
else if (reverse == false)
{
GetComponent<Rigidbody2D>().gravityScale = 1.8f;
changeGravity = false;
}
changeGravity = false;
}
if (flap == true)
{
if (reverse == true)
{
GetComponent<Rigidbody2D>().gravityScale = -0.8f;
GetComponent<Rigidbody2D>().velocity = -(flapVelocity) / 2;
}
else if (reverse == false)
{
GetComponent<Rigidbody2D>().gravityScale = 0.8f;
GetComponent<Rigidbody2D>().velocity = flapVelocity / 2;
}
//GetComponent<Rigidbody2D>().AddForce(Vector2.up * flapSpeed);
GetComponent<Rigidbody2D>().AddForce(Vector2.right * ForwardSpeed);
flap = false;
}
transform.Rotate(0, 0, -150 * Time.deltaTime);
}
Comment