Question
This is coded in C# and this uses the Unity game-making engine. Hello! I am making a game and I am trying to figure out
This is coded in C# and this uses the Unity game-making engine.
Hello! I am making a game and I am trying to figure out how to fix my camera. I am making a game with a camera very similar to that of Pokemon Go. I currently have almost everything I want implemented, the biggest issue I'm facing however is the 360 degree rotation of the camera. There are other programs I have written but they need not be referenced for the issue to be resolved. The camera currently has a 180 degree radius with a single finger swipe. When you take your finger and swipe from the left of the player to the right, and vice-versa, the player's camera needs to rotate 360 degrees, but instead it starts turning backwards at 180 degrees if you keep swiping and doesn't want to rotate 360 degrees. It will keep turning if you let your finger go and keep swiping mutiple times, but I need to have it to where you can do it in one swipe.
Below is my code, if you think there's an addition or removal I should make, let me know. Thank you!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camera_movement : MonoBehaviour
{
[SerializeField] private Camera cam;
[SerializeField] public Transform player;
[SerializeField] public static string camera_state = "low"; //all camera_states: "low" | "high"
public float distance_to_target;
//"low" swipe var's
private Vector2 previous_position;
private GameObject camera_parent;
public float rotation_speed;
// Pinch zoom variables
private float previousTouchDeltaMagnitude;
private float currentTouchDeltaMagnitude;
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" or City View
case "low":
swipe_control();
camera_dist();
break;
//"high" or Empire 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
if (x_difference != 0) //between the current finger location and the last frame.
{
camera_parent.transform.Rotate(Vector3.up * x_difference * rotation_speed);
}
previous_position = touch.position;
}
if (Input.touchCount == 2)
{
Touch touch1 = Input.GetTouch(0);
Touch touch2 = Input.GetTouch(1);
Vector2 touch1_previous_pos = touch1.position - touch1.deltaPosition;
Vector2 touch2_previous_pos = touch2.position - touch2.deltaPosition;
float prev_magnitude = (touch1_previous_pos - touch2_previous_pos).magnitude;
float current_magnitude = (touch1.position - touch2.position).magnitude;
float difference = current_magnitude - prev_magnitude;
if (Mathf.Abs(difference) > 0.01f)
{
cam.transform.Translate(new Vector3(0, 0, difference * 0.1f));
distance_to_target = Mathf.Clamp(distance_to_target - difference * 0.07f, 40f, 100f); //adjust speeds here
}
}
}
if (touch.phase == TouchPhase.Ended && touch.fingerId == 0)
{
previous_position = touch.position;
}
}
}
camera_dist();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started