How to move a player with a joystick and rotate camera at the same time?

Hello,

I am making an Android game and I have a joystick to move the player, and I have this script to rotate the camera:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class RotateCam : MonoBehaviour {

    private Touch initTouch = new Touch();
    public Camera cam;
   
    private float rotX = 0f;
    private float rotY = 0f;
    private Vector3 origRot;
   
    public float rotSpeed = 0.5f;
    public float dir = -1;
   
    public bool swipe = true;
   
    void Start()
    {
        origRot = cam.transform.eulerAngles;
        rotX = origRot.x;
        rotY = origRot.y;
    }
   
    void FixedUpdate() {
        foreach(Touch touch in Input.touches)
        {
            if(touch.phase == TouchPhase.Began)
            {
                initTouch = touch;
            }
            else if(touch.phase == TouchPhase.Moved)
            {
                //Swiping
                if(swipe)
                {
                float deltaX = initTouch.position.x - touch.position.x;
                float deltaY = initTouch.position.y - touch.position.y;
                rotX -= deltaY * Time.deltaTime * rotSpeed * dir;
                rotY += deltaX * Time.deltaTime * rotSpeed * dir;
                    rotX = Mathf.Clamp(rotX,-45f,45f);
                    cam.transform.eulerAngles = new Vector3(rotX,rotY,0f);
                }
            }
            else if(touch.phase == TouchPhase.Ended)
            {
                initTouch = new Touch();
            }
        }
    }
   
    public void SwipeOn()
    {
        swipe = true;
    }
   
    public void SwipeOff()
    {
        swipe = false;
    }
}

What is happening is that I am unable to move the player and rotate the camera at the same time.

In other words, I am not able to swipe twice simultaneously.

I need some help.

Thanks.

So is it the same joystick or is it like the controllers with two joysticks? One for camera and one for movement?

@Basen1
No, I have a joystick used just for moving the player. And for rotation, I swipe the screen to rotate the camera.

Oh ok then, so the joystick is a virtual one I am guessing and a GUI element so you could just use OnGUI for that one?

@Basen1
Yea It is a virtual joystick. I got it from the asset store here:
My problem is not with the joystick.
I am able to move the player and I am also able to rotate the camera, but each one on its own.
But if I try to move the player and rotate the camera at the same time, nothing happens.
Again my problem is performing two swipes at the same time( the first swipe to move the player, and the 2nd is to rotate the camera).

Had the same problem…
someone found solution?

After Ten Years here I am commenting has anyone found the solution

void FixedUpdate() {

foreach(Touch touch in Input.touches)
{
if(touch.phase == TouchPhase.Began && touch.position.x > (ScreenWidth/2))
{
initTouch = touch;
}

else if(touch.phase == TouchPhase.Moved && touch.position.x > (ScreenWidth/2))
{
// Do your Job
float deltaX = initTouch.position.x - touch.position.x ;
float deltaY = initTouch.position.y - touch.position.y;

xRotation -= deltaY * Time.deltaTime * RotSpeed * -1 ;
yRotation += deltaX * Time.deltaTime * RotSpeed * -1;

transform.eulerAngles = new Vector3(xRotation , 0f , 0f);
xRotation = Mathf.Clamp(xRotation , -70f , 70f);

PlayerBody.Rotate(Vector3.up * yRotation);

}
else if (touch.phase == TouchPhase.Ended && touch.position.x > (ScreenWidth/2))
{
initTouch = new Touch() ;

}

}

I have done this but by doing so I cant move my player Independently of where the player is facing

HUNDREDS of solutions have been found, but PLEASE… DO NOT necro-post threads.

It’s against forum rules for one. It pollutes the knowledge space with unrelated information for two.

If you have a question, please make your own post. It’s FREE!

When you post, keep this in mind:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand errors in general:

https://forum.unity.com/threads/assets-mouselook-cs-29-62-error-cs1003-syntax-error-expected.1039702/#post-6730855

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

3 Likes