Skip to content

Instantly share code, notes, and snippets.

@fuwac
Last active June 19, 2017 23:39
Show Gist options
  • Save fuwac/5c9f1bea805e7404552dd852e27e04e2 to your computer and use it in GitHub Desktop.
Save fuwac/5c9f1bea805e7404552dd852e27e04e2 to your computer and use it in GitHub Desktop.
Unityでカメラをスクロールさせるやつ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scroll : MonoBehaviour {
private float touch_start_x; // タッチ開始X座標
private float touch_end_x; // タッチ終了X座標
private bool during_touch; // タッチ中かな?
public Camera cam; // カメラ(外から入れる)
// Use this for initialization
void Start () {
this.during_touch = false;
this.touch_start_x = 0;
this.touch_end_x = 0;
}
// スクロール処理
private void Scroll()
{
// タッチ中だったら
if(during_touch)
{
// 今回の座標を取得
this.touch_end_x = cam.ScreenToWorldPoint(Input.mousePosition).x;
// 前回の座標と違ったら
if (this.touch_start_x != this.touch_end_x)
{
// カメラをその分動かすよ
this.cam.transform.position += new Vector3(this.touch_end_x - this.touch_start_x, 0, 0);
}
// 今回の座標を保存
this.touch_start_x = cam.ScreenToWorldPoint(Input.mousePosition).x;
}
// タッチ開始
if (Input.GetMouseButtonDown(0))
{
this.during_touch = true;
// 今回の座標を保存
this.touch_start_x = cam.ScreenToWorldPoint(Input.mousePosition).x;
}
// タッチ終了
if (Input.GetMouseButtonUp(0))
{
this.during_touch = false;
this.touch_start_x = 0;
this.touch_end_x = 0;
}
}
// Update is called once per frame
void Update () {
this.Scroll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment