- Home /
How do I move my train with a slider, and not W & S
You know how mobile train simulators use a slider to move the train? I would like to emulate it. I found this script on one of my VERY old simulators. It rotates sphere collides, which in turn moves the train. I like the way the train gradually accelerates, and decelerates realistically, and brakes realistically. However, as I want to adapt this game for mobile, I need to make it so the train moves with a UI slider.
using UnityEngine;
using System.Collections;
public class Drive_Wheel : MonoBehaviour {
public bool Drive_Flag = true ;
public float Max_Torque = 800.0f ;
public float Accelerate_Rate = 200.0f ;
public float Brake_Drag = 5.0f ;
public float Idle_Drag = 0.1f ;
Rigidbody This_RigidBody ;
float Target_Torque ;
float Current_Torque ;
void Start () {
This_RigidBody = this.GetComponent<Rigidbody>() ;
This_RigidBody.maxAngularVelocity = 50.0f ;
This_RigidBody.angularDrag = Idle_Drag ;
// Layer settings.
this.gameObject.layer = 8 ;
Physics.IgnoreLayerCollision ( 0 , 8 , true ) ;
Physics.IgnoreLayerCollision ( 8 , 8 , false ) ;
}
void Update () {
// Accelerate
if ( Drive_Flag ) {
if ( Input.GetAxis ( "Vertical" ) > 0.0f ) {
Target_Torque = Mathf.Lerp ( 0.0f , Max_Torque , Input.GetAxis ( "Vertical" ) ) ;
}
}
// Brake
if ( Input.GetAxis ( "Vertical" ) < 0.0f ) {
Target_Torque = 0.0f ;
This_RigidBody.angularDrag = -Input.GetAxis ( "Vertical" ) * Brake_Drag ;
}
// Idle
if ( Input.GetAxis ( "Vertical" ) == 0.0f ) {
Target_Torque = 0.0f ;
This_RigidBody.angularDrag = Idle_Drag ;
}
//
if ( Current_Torque < Target_Torque ) {
Current_Torque = Mathf.MoveTowards ( Current_Torque , Target_Torque , Accelerate_Rate * Time.deltaTime ) ;
} else if ( Current_Torque > Target_Torque ) {
Current_Torque = Target_Torque ;
}
}
void FixedUpdate () {
GetComponent<Rigidbody>().AddRelativeTorque ( Current_Torque , 0 , 0 ) ;
}
}
Thanks guys!
Answer by Harinezumi · Feb 27, 2018 at 10:13 AM
If I understand correctly, you want to control the desired speed via a slider. In that case, just replace Input.GetAxis("Vertical")
with slider.value
, assign the UI Slider to the member slider
of the script, and set the Slider's range to -1, 1 (or whatever range you would like).
wait so if i'm right, my script should look like this, right?
if ( Drive_Flag ) {
if ( accel.value > 0.0f ) {
Target_Torque = $$anonymous$$athf.Lerp ( 0.0f , $$anonymous$$ax_Torque , accel.value ) ;
}
}
With accel being a public slider. However, it does not move at all.
Yes, this code looks fine for setting a target torque using the slider.
Have you assigned your slider to the public field accel
? Is Drive_Flag
true
? Are you using the variable Target_Torque
?
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Issue with implementing my own slider script. 1 Answer
Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value 1 Answer
Move Slider-value to middle value when released 1 Answer
Why hip will move upward and downward automatically when start to drag slider at x,y and z axis? 0 Answers