How to make an object keep move when pressing a button?
I wan't it so that whenever i press a button my car will turn left and it does do that currently, but only for one frame. It doesn't matter if i hold down the button it still only goes one frame. I wan't my game to be for Android so i didn't use the OnClick function instead i used the "Pointer Down" function from the event trigger. I have tried this with many different functions so i am certain that this is because of the script.
Here is the script (i have marked the function i used in the event trigger (it's at the bottom) )
using UnityEngine; using System.Collections;
public class Car2DController : MonoBehaviour {
public float power = 3;
public float maxspeed = 5;
public float turnpower = 2;
public float friction = 3;
public Vector2 curspeed ;
Rigidbody2D rigidbody2D;
ScoreManager scoreManager;
// Use this for initialization
void Start () {
rigidbody2D = GetComponent<Rigidbody2D>();
scoreManager = GameObject.FindGameObjectWithTag ("ScoreManager").GetComponent<ScoreManager> ();
}
void FixedUpdate()
{
rigidbody2D.AddForce(transform.up * power);
rigidbody2D.drag = friction;
curspeed = new Vector2(rigidbody2D.velocity.x, rigidbody2D.velocity.y);
if (curspeed.magnitude > maxspeed)
{
curspeed = curspeed.normalized;
curspeed *= maxspeed;
}
if (Input.GetKey(KeyCode.Space))
{
transform.Rotate(Vector3.forward * turnpower);
}
noGas();
}
void noGas()
{
bool gas;
if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
{
gas = true;
}
else
{
gas = false;
}
if (!gas)
{
rigidbody2D.drag = friction * 2;
}
}
void OnTriggerEnter2D(Collider2D hit)
{
if (hit.CompareTag ("Goal"))
{
scoreManager.scoreCount += 1;
power += 3;
maxspeed += 3;
}
}
//FUNCTION I USED
public void Turn()
{
transform.Rotate(Vector3.forward * turnpower);
}
}
Comment