- Home /
I want my vehicle to keep moving left/right while the buttons are held down?
I want to make it so that my vehicle can move left and right more than just once (currently, it only moves once each time the button is hit. I want it to keep moving while the button is held down, to make the movement more fluid). Here's my current script for the vehicle (C#):
using UnityEngine;
using System.Collections;
public class playerScript : MonoBehaviour {
float vehicleSpeedLeftRight = 10f;
float vehicleSpeed = 10f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Horizontal") )
{
transform.Translate( vehicleSpeedLeftRight * Time.deltaTime * Input.GetAxis( "Horizontal" ), 0, 0 );
}
}
void FixedUpdate()
{
rigidbody.AddForce(-transform.forward * vehicleSpeed, ForceMode.VelocityChange);
rigidbody.velocity = -transform.forward * vehicleSpeed;
}
}
(The reason there's a negative sign next to the transform.forward is because for some reason it was going backwards instead of forwards, so I used that to make it un-inverted)
No worries, the Unity Scripting Reference is useful for finding commands or how to use them, just use the search bar in the top-left.
Input (scroll down to class functions) : http://docs.unity3d.com/Documentation/ScriptReference/Input.html
Answer by shane.rachel · Dec 09, 2012 at 03:41 AM
Input.GetButtonDown only passes true when you press down the button. Input.GetButton passes true the entire time you hold down the button.
your welcome, alucardj already answered it but I figured I would explain the difference, they're both useful in different situations.
if you thought they were useful you should up vote me :)
I tried to upvote you but it said I can't lol. Not sure why
hmmmm lol well that's too bad, maybe it;s just isn't saying you did but you actually did.
you need 15+ karma from memory to upvote an answer on any question. When answer is accepted, you both also gain karma.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Separate Objects after Mouse Clicking in Unity 5.6.4 1 Answer