0
votes

I'm working within Unity with the new UI 4.6 system.

Basically I have a very simple way of calculating the players movement (It only moves on the x axis). Due to this I'm only using "Horizontal" to get the movement at the moment.

What I need is a way to connect the value of the players movement to a slider so that when you drag it left it goes left and when you leave go it stops moving and the same for the right hand side.

I'm still new to programming and have looked all over to get a rough answer and I'm struggling.

Here are the two ways that I can move the player: The first is with velocity that I recently tried to do but then got lost as it will only send the player 1 way depending on the value of -1 or 1. I call the following statement in the FixedUpdate function.

    public 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;
}

Before trying to get a mobile type of movement I was simply using this:

        Vector3 newPosition = transform.position;
    newPosition.x += Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
    transform.position = newPosition;

This was a very simple way to move the character but I'm unsure if I can assign it to the slider.

I know that the methods must be public void for an UI object to access them etc, it's just assigning the variables to the right places that I'm struggling with.

Thank you very much for your time.

1
So you want a slider to represent a character's horizontal position on screen? Or do you want a slider to control its velocity?Zach Thacker
I want the position controlled with the slider. Imagine a space invaders game how the character moves left to right. Just like that but using a slider. Thank you for responding.CardDeath

1 Answers

0
votes

Might I suggest using something like this.

  public class TouchPad : MonoBehaviour, IPointerUpHandler, IPointerDownHandler, IDragHandler 
    {
        public Vector2 virtualAxes;

        private Vector2 imagePad;
void Start()
    {
        imagePad = GetComponent<RectTransform>().sizeDelta;
    }
public void OnDrag(PointerEventData data)
    {
        imageColor.color = new Color (1f, 1f, 1f, 1f);

        int deltaPositionX = (int)(data.position.x - transform.position.x);
        deltaPositionX = Mathf.Clamp (deltaPositionX, -(int)(imagePad.x/2), (int)(imagePad.x/2));

        int deltaPositionY = (int)(data.position.y - transform.position.y);
        deltaPositionY = Mathf.Clamp (deltaPositionY, -(int)(imagePad.y/2), (int)(imagePad.y/2));

        virtualAxes = new Vector2 (deltaPositionX / (imagePad.x/2), deltaPositionY / (imagePad.y/2));
    }

    public void OnPointerUp(PointerEventData data)
    {
        virtualAxes = Vector2.zero;
        imageColor.color = new Color (1f, 1f, 1f, 0.15f);
    }

    public void OnPointerDown(PointerEventData data)
    {

    }

And then to control it you could use something like this.

public class ControlPad : MonoBehaviour 
{
    public TouchPad aPad;


    public float speed = 5f;
    public float speedRotate = 150f;

    void Update ()
    {
        transform.Translate (aPad.virtualAxes.x* Time.deltaTime * speed, 0 ,0f);

    }   

}

Now you can simply created a UI elements that is as big as your slider and when you drag across it with a finger or a mouse you will move an object on the x axis.