Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do you move a character using a slider?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Nosmo, Mar 21, 2019.

  1. Nosmo

    Nosmo

    Joined:
    Sep 12, 2014
    Posts:
    21
    I trying to move a simple character using a slider but with what i have now I can move it using the keys and all forward momentum is automatic.

    When the section for the slider is active it moves the player forward. as much as the slider is set for.

    here is the code so far:

    public class PlayerMotor : MonoBehaviour {

    1. private CharacterController charController;
    2. private Vector3 moveVector;
    3. public float forwardSpeed = 5.0f;
    4. public float leftRightSpeed = 5.0f;
    5. public GameObject Player;
    6. // Start is called before the first frame update
    7. public void Start()
    8. {
    9. charController = GetComponent<CharacterController>();
    10. }
    11. // Update is called once per frame
    12. public void Update(float leftRightSpeed) //addition
    13. {
    14. MovePlayer();
    15. }
    16. // This moves the player side to side using the arrow keys
    17. public void MovePlayer() //addtion
    18. {
    19. moveVector = Vector3.zero;
    20. moveVector.x = Input.GetAxisRaw("Horizontal") * leftRightSpeed; //left-right
    21. moveVector.z = forwardSpeed; //forward
    22. charController.Move(moveVector * Time.deltaTime);
    23. }
    24. // This moves the play with the slider. it's meant to be side to side but it moves it forwards
    25. public void MovePlayer() //addtion
    26. {
    27. Vector3 pos = Player.transform.position; //addtion
    28. pos.x = leftRightSpeed; //addtion
    29. Player.transform.position = pos; //addtion
    30. Debug.Log("moving sideways");
    31. moveVector.z = forwardSpeed; //forward
    32. Debug.Log("moving forward");
    33. charController.Move(moveVector * Time.deltaTime);
    34. }
    }

    Any suggestions as to how to resolve this?