Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by subzer0, Jul 25, 2017.

Thread Status:
Not open for further replies.
  1. subzer0

    subzer0

    Joined:
    Dec 10, 2010
    Posts:
    94
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class RotateCam : MonoBehaviour {
    8.  
    9.     private Touch initTouch = new Touch();
    10.     public Camera cam;
    11.    
    12.     private float rotX = 0f;
    13.     private float rotY = 0f;
    14.     private Vector3 origRot;
    15.    
    16.     public float rotSpeed = 0.5f;
    17.     public float dir = -1;
    18.    
    19.     public bool swipe = true;
    20.    
    21.     void Start()
    22.     {
    23.         origRot = cam.transform.eulerAngles;
    24.         rotX = origRot.x;
    25.         rotY = origRot.y;
    26.     }
    27.    
    28.     void FixedUpdate() {
    29.         foreach(Touch touch in Input.touches)
    30.         {
    31.             if(touch.phase == TouchPhase.Began)
    32.             {
    33.                 initTouch = touch;
    34.             }
    35.             else if(touch.phase == TouchPhase.Moved)
    36.             {
    37.                 //Swiping
    38.                 if(swipe)
    39.                 {
    40.                 float deltaX = initTouch.position.x - touch.position.x;
    41.                 float deltaY = initTouch.position.y - touch.position.y;
    42.                 rotX -= deltaY * Time.deltaTime * rotSpeed * dir;
    43.                 rotY += deltaX * Time.deltaTime * rotSpeed * dir;
    44.                     rotX = Mathf.Clamp(rotX,-45f,45f);
    45.                     cam.transform.eulerAngles = new Vector3(rotX,rotY,0f);
    46.                 }
    47.             }
    48.             else if(touch.phase == TouchPhase.Ended)
    49.             {
    50.                 initTouch = new Touch();
    51.             }
    52.         }
    53.     }
    54.    
    55.     public void SwipeOn()
    56.     {
    57.         swipe = true;
    58.     }
    59.    
    60.     public void SwipeOff()
    61.     {
    62.         swipe = false;
    63.     }
    64. }
    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.
     
  2. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    So is it the same joystick or is it like the controllers with two joysticks? One for camera and one for movement?
     
  3. subzer0

    subzer0

    Joined:
    Dec 10, 2010
    Posts:
    94
    @Basen1
    No, I have a joystick used just for moving the player. And for rotation, I swipe the screen to rotate the camera.
     
  4. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    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?
     
  5. subzer0

    subzer0

    Joined:
    Dec 10, 2010
    Posts:
    94
    @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).
     
  6. elikhaim

    elikhaim

    Joined:
    Oct 8, 2014
    Posts:
    1
    Had the same problem..
    someone found solution?
     
  7. sweeka

    sweeka

    Joined:
    Feb 8, 2021
    Posts:
    2
    After Ten Years here I am commenting has anyone found the solution
     
  8. sweeka

    sweeka

    Joined:
    Feb 8, 2021
    Posts:
    2
    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
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,809
    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/ass...3-syntax-error-expected.1039702/#post-6730855

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

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    Cyber-Dog, zombiegorilla and JoNax97 like this.
Thread Status:
Not open for further replies.