- Home /
4.6 UI Player Movement with Slider.
Hey there, I'm wanting the players velocity to be controls by a slider. However this may have to be done by using location rather than velocity. I'm pretty new to physics within unity and the new UI system like most people. This is my player movement script at the moment:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Player : MonoBehaviour {
private float moveSpeed = 20f;
private int playerHitpoints = 3;
private GameObject hitPartical;
private GameObject deathPartical;
private float hozInput;
public GameObject[] healthIcons = new GameObject[3];
Transform myTrans;
Rigidbody myBody;
// Use this for initialization
void Start () {
//Says memory.
myTrans = this.transform;
myBody = this.rigidbody;
healthIcons [0].SetActive (true);
healthIcons [1].SetActive (true);
healthIcons [2].SetActive (true);
deathPartical = (GameObject)(Resources.Load("Partical Systems/PS_Enemy_Dead"));
hitPartical = (GameObject)(Resources.Load("Partical Systems/PS_Enemy_Hit"));
}
// Update is called once per frame
void FixedUpdate () {
//If it's not any one of these devices then keyboard controls will work.
#if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT
//Calls the movement function and takes a float which is the Input.GetAxis short cut.
Movement (Input.GetAxis ("Horizontal"));
#else
Movement (hozInput);
#endif
}
void Movement(float horizontalInput)
{
//Stores the velocity in a V3.
Vector3 moveVel = myBody.velocity;
//The x axis is the value passed intimes by movespeed.
moveVel.x = horizontalInput * moveSpeed;
//The value from the v3 is stored back into velocity.
myBody.velocity = moveVel;
}
public void StartMoving(float horizontalInput)
{
hozInput = horizontalInput;
}
Im aware that this doesn't work because it simply takes a value in hozInput and will just set the velocity in 1 direction on the X axis. I need it to maintain the same velocity value but have it go from positive to minus in the X and have the player stop when the player stops sliding.
Any ideas?
Thank you.
Comment