Android| Camera scrolling
Hello everyone! I'm new to programming with android on unity. And I'm having a small issue. What i want to happen is a smooth pan around the map. But with my current script it seems to not be smooth at all, it works but not the way i wanted it to. Here is my code
public void moveMap()
{
if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
{
touchPos = Input.GetTouch(0).position;
}
else if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(0).phase != TouchPhase.Stationary)
{
var newPos = Input.GetTouch(0).position;
var movePos = (touchPos - newPos) * 0.1f * Time.deltaTime;
transform.Translate(Mathf.Clamp( movePos.x, -0.5f, 0.5f), Mathf.Clamp(movePos.y, -0.5f, 0.5f), 0);
transform.position = new Vector3(Mathf.Clamp(transform.position.x, minX, maxX), Mathf.Clamp(transform.position.y, minY, maxY), transform.position.z);
Debug.Log(movePos.x);
}
}
Here is a Gif of what i have now. i know a gif is not gonna represent what I'm feeling on my end but at least it gives you more of an explanation of what I'm trying to achieve.
https://gyazo.com/d36c9e709ebaa24361fd89fea45f0962
Thanks everyone!
Answer by Creepercatss · Apr 07, 2017 at 08:21 AM
I found the answer! if anyone needs it, it's right here!
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public float speed = 0.1F;
void Update() {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
}
}
}
Thanks all!
Your answer
Follow this Question
Related Questions
Touch on 2D gameobject: should i use a GUI button? 1 Answer
Problem with swipe and player jump 2 Answers
How to optimize touch input script 0 Answers
Please Help Im confuse im beginner in using unity. 1 Answer
When The Screen Is Touched Jump 0 Answers