Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This uses the Unity game engine and is coded in C# Hello! I am trying to implement a way to have a two finger pinch

This uses the Unity game engine and is coded in C#

Hello! I am trying to implement a way to have a two finger "pinch" zoom in this code for a game I'm making. Below is my current camera movement code. This is for a game that has a camera similar to pokemon go's camera where you can use two fingers to zoom in and zoom out from the player's third person perspective. Below is my current code for the project, there are other programs I have written but you need not need any information outside of this program. The line where it says if(touchcount == 2){} is how you start it, but I don't know where to go from there. I have a way for the player to have an (almost) 360 degree radius around the player in the if(touch count > 0 code) as a sort of reference.

I am in no hurry so take your time.

Thank you!!

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class camera_movement : MonoBehaviour

{

[SerializeField] private Camera cam;

[SerializeField] public Transform player;

//all camera_states: "low" | "high"

[SerializeField] public static string camera_state = "low";

public float distance_to_target;

//"low" swipe var's

private Vector2 previous_position;

private GameObject camera_parent;

public float rotation_speed;

void Start()

{

Transform originalParent = transform.parent;

camera_parent = new GameObject ("camera");

transform.parent = camera_parent.transform;

camera_parent.transform.parent = originalParent;

camera_dist();

}

void Update()

{

switch (camera_state)

{

//"low" view

case "low":

swipe_control();

camera_dist();

break;

//"high" view

case "high":

swipe_control();

break;

}

}

void camera_dist()

{

cam.transform.position = player.position;

camera_parent.transform.position = player.position;

int buffer_up = 20;

cam.transform.Translate(new Vector3(0, buffer_up, - distance_to_target));

}

void swipe_control()

{

if (Input.touchCount > 0)

{

foreach (Touch touch in Input.touches)

{

if (touch.phase == TouchPhase.Began && touch.fingerId == 0)

{

previous_position = touch.position;

}

if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)

{

if (touch.fingerId == 0)

{

float x_difference = touch.position.x - previous_position.x; //t$$anonymous$$s calculates the horizontal distance between the current finger location and the location last frame.

if (x_difference != 0)

{

camera_parent.transform.Rotate (Vector3.up * x_difference * rotation_speed);

}

previous_position = touch.position;

}

}

if (touch.phase == TouchPhase.Ended && touch.fingerId == 0)

{

previous_position = touch.position;

}

}

}

if (Input.touchCount == 2)

{

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

Students also viewed these Databases questions

Question

Find y'. y= |x + X (x) (x) X 1 02x+ 2x 1 O 2x + 1/3 Ex 2x +

Answered: 1 week ago