Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

how to contol third person camera using touch

i used a simple joystick pack to move caracter then i made a transparent image i added a script in it Fixed touch field

using UnityEngine;

using UnityEngine.EventSystems;


public class FixedTouchField : MonoBehaviour, IPointerDownHandler, IPointerUpHandler

{

  [HideInInspector]

  public Vector2 TouchDist;

  [HideInInspector]

  public Vector2 PointerOld;

  [HideInInspector]

  protected int PointerId;

  [HideInInspector]

  public bool Pressed;


  // Use this for initialization

  void Start()

  {


  }


  // Update is called once per frame

  void Update()

  {

    if (Pressed)

    {

      if (PointerId >= 0 && PointerId < Input.touches.Length)

      {

        TouchDist = Input.touches[PointerId].position - PointerOld;

        PointerOld = Input.touches[PointerId].position;

      }

      else

      {

        TouchDist = new Vector2(Input.mousePosition.x, Input.mousePosition.y) - PointerOld;

        PointerOld = Input.mousePosition;

      }

    }

    else

    {

      TouchDist = new Vector2();

    }

  }


  public void OnPointerDown(PointerEventData eventData)

  {

    Pressed = true;

    PointerId = eventData.pointerId;

    PointerOld = eventData.position;

  }



  public void OnPointerUp(PointerEventData eventData)

  {

    Pressed = false;

  }


}



then i added this two lines of code to control the camera with touch

 protected float camerangle;

  protected float cameraanlglespee

 camerangle += touchfield.TouchDist.x * cameraanlglespeed

Camera.main.transform.position = transform.position + Quaternion.AngleAxis(camerangle, Vector3.up) * new Vector3(0, 3, 4);

    Camera.main.transform.rotation = Quaternion.LookRotation(transform.position+Vector3.up*2f - Camera.main.transform.position,Vector3.up);

no error was found and everything is ok but i had this error in the unity console

NullReferenceException: Object reference not set to an instance of an object

control.Update () (at Assets/control.cs:25)

Answers

  • HNJHNJ Member

    Can you post the screenshot of your code opened in IDE (so i can see the line numbers) and Unity Console

Sign In or Register to comment.