1
\$\begingroup\$

My game features a top down camera that follows the player as he moves via virtual joystick on a cellphone screen. I want the movement of the player to always walk forward and the direction will be the only thing varying, thus allowing the character to move around the level. I currently have two scripts, the first one is the user's input on the virtual joystick which returns two floats. This values are used in the second script which is character controller. Right now the character moves yet it won't rotate to face the direction where the joystick is moving, when I try to apply rotation to the character the movement just goes crazy. Here are the two scripts. What fix can I use to achieve the desired movement?.

    public class Joystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler{
    private Animator anim;
    private CharacterController controller;
    public float speed = 6.0F;
    public float turnSpeed = 60.0f;
    private Vector3 moveDirection = Vector3.zero;
    public Joystick inputJoystick;
    private float horizontal = 0.0f;
    private float vertical = 0.0f;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        anim = gameObject.GetComponentInChildren<Animator>();
    }

    void Update()
    {

        horizontal = inputJoystick.horizontal();
        vertical = inputJoystick.vertical();

        moveDirection = new Vector3(horizontal, 0, vertical);
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;

        controller.Move(moveDirection * Time.deltaTime);



        //transform.Rotate(0,turnSpeed * horizontal * Time.deltaTime,0);
 }    

.

    public class Joystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler{

    public Image back;
    public Image front;
    private Vector3 inputVector;


    public virtual void OnDrag(PointerEventData data)
    {
        Vector2 position;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(back.rectTransform, data.position, data.pressEventCamera, out position))
        {
            position.x = (position.x / back.rectTransform.sizeDelta.x);
            position.y = (position.y / back.rectTransform.sizeDelta.y);

            inputVector = new Vector3(position.x * 2 + 1, 0, position.y * 2 - 1);
            inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;

            front.rectTransform.anchoredPosition = new Vector3(inputVector.x * (back.rectTransform.sizeDelta.x / 2), inputVector.z * (back.rectTransform.sizeDelta.y / 2));

        }
    }
    public virtual void OnPointerDown(PointerEventData data)
    {
        OnDrag(data);
    }

    public virtual void OnPointerUp(PointerEventData data)
    {
        inputVector = Vector3.zero;
        front.rectTransform.anchoredPosition = Vector3.zero;
    }

    public float horizontal()
    {
        return inputVector.x; 
    }

    public float vertical()
    {
        return inputVector.z;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.