Search Unity

Touch Camera Look

Discussion in 'Scripting' started by sStarS, Jul 3, 2020.

  1. sStarS

    sStarS

    Joined:
    Mar 27, 2020
    Posts:
    19
    hello, if anybody can help me I found this touchfield script that works like a charm on a 3rd person camera my request will be if anybody can show me how to make the camera move up and down, currently only move left and right but since i'm working on a RPG game I need all 4 directions for the camera so I can pick up items and aim my bow or magic

    here are the scripts...
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Security.Cryptography;
    5. using UnityEngine;
    6. using UnityStandardAssets.Characters.ThirdPerson;
    7.  
    8. public class ThirdPersonInput : MonoBehaviour
    9. {
    10.     public FixedJoystick LeftJoystick;
    11.     public FixedButton Button;
    12.     public FixedTouchField Touchfield;
    13.  
    14.     protected ThirdPersonUserControl Control;
    15.     protected float CameraAngle;
    16.     protected float CameraAngleSpeed = 0.3f;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         Control = GetComponent<ThirdPersonUserControl>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         Control.m_Jump = Button.Pressed;
    28.         Control.h = LeftJoystick.Direction.x;
    29.         Control.v = LeftJoystick.Direction.y;
    30.         CameraAngle += Touchfield.TouchDist.x * CameraAngleSpeed;
    31.         Camera.main.transform.position = transform.position + Quaternion.AngleAxis(CameraAngle, Vector3.up)* new Vector3(0, 1, -2);
    32.         Camera.main.transform.rotation = Quaternion.LookRotation(transform.position + Vector3.up * 1f - Camera.main.transform.position, Vector3.up);
    33.     }
    34. }
    35.  
    and for the touchfield...

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. public class FixedTouchField : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    5. {
    6.     [HideInInspector]
    7.     public Vector2 TouchDist;
    8.     [HideInInspector]
    9.     public Vector2 PointerOld;
    10.     [HideInInspector]
    11.     protected int PointerId;
    12.     [HideInInspector]
    13.     public bool Pressed;
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.     }
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (Pressed)
    22.         {
    23.             if (PointerId >= 0 && PointerId < Input.touches.Length)
    24.             {
    25.                 TouchDist = Input.touches[PointerId].position - PointerOld;
    26.                 PointerOld = Input.touches[PointerId].position;
    27.             }
    28.             else
    29.             {
    30.                 TouchDist = new Vector2(Input.mousePosition.x, Input.mousePosition.y) - PointerOld;
    31.                 PointerOld = Input.mousePosition;
    32.             }
    33.         }
    34.         else
    35.         {
    36.             TouchDist = new Vector2();
    37.         }
    38.     }
    39.     public void OnPointerDown(PointerEventData eventData)
    40.     {
    41.         Pressed = true;
    42.         PointerId = eventData.pointerId;
    43.         PointerOld = eventData.position;
    44.     }
    45.  
    46.     public void OnPointerUp(PointerEventData eventData)
    47.     {
    48.         Pressed = false;
    49.     }
    50. }
    51.  
     
    Last edited: Jul 3, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    sStarS likes this.
  3. sStarS

    sStarS

    Joined:
    Mar 27, 2020
    Posts:
    19
    thank you and sorry
    and I don't have errors or anything I just don't know how to make it look up and down aside from the left right input ( my priority right now is to launch my project and after i'll pay for a proper class )

    like I said first post everything works fine I just need the camera to also look up and down aside from left and right which are working just fine
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Time to start inserting strategic
    Debug.Log("I am here!");
    type calls throughout the code to find out:

    1. is code even running?
    2. what parts are running
    3. what are values at different parts?
    etc

    Just basic debugging really.
     
  5. sStarS

    sStarS

    Joined:
    Mar 27, 2020
    Posts:
    19
    maybe i'm not explaining right, the code does what it's supposed to do except the camera only moves left and right I need somebody to complete the code so that the camera will also move up and down , I tried something but all it did was turn the camera upside down lol since I have no idea how the touch field was written I don't know the logic behind it so I cant modify it myself
    in simpler words " mouse aim camera" moves in whichever direction you move your mouse
    I want the same movements on the touchfield but in this script the camera only moves left and right so is incomplete like I said in first post I need to be able to pick up things and talk to npc so I need free movements so I can point wherever I need to I cant pick up things from the floor if I cant turn my camera downwards
     
    Last edited: Jul 6, 2020
  6. sStarS

    sStarS

    Joined:
    Mar 27, 2020
    Posts:
    19
    is fine ill just highlight objects with just left and right movements and open a different ui window to pick up stuff instead of aiming directly on them to pick up, seems nobody cares to help complete a script without being payed first , it kills the purpose of this forum, lock this thread please