Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ControllerUI : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler{
float speed = 3f;
float pulusSpeed=1;
Vector2 startPosition;
Vector2 endPosition;
float radius=50;
bool isMove = false;
Transform charactor;
Animator animator;
float distance;
private float deg = 0;
float deltaTime = 0.1f;
void Start(){}
void Update(){
Vector3 velocity = new Vector3(0, 0, 1);
// キャラクターのローカル空間での方向に変換
velocity = charactor.TransformDirection(velocity);
float speedCache = speed *pulusSpeed* Time.deltaTime;
// キャラクターの移動
charactor.localPosition += velocity * speedCache;
// ドラックが開始したとき
public void OnBeginDrag(PointerEventData eventData){
startPosition = transform.position;
isMove = true;
// ドラック中
public void OnDrag(PointerEventData eventData){
distance = Vector2.Distance(startPosition,eventData.position);
if(distance<radius)transform.position = eventData.position;
else distance=radius;
deg = GetAngle(startPosition,eventData.position);
charactor.eulerAngles = new Vector3(0,deg,0);
// ドラックが終了したとき
public void OnEndDrag(PointerEventData eventData){
transform.localPosition = new Vector2(0,0);
distance =0;
isMove = false;
float GetAngle(Vector2 start,Vector2 target){
Vector2 dt = target - start;
float rad = Mathf.Atan2 (dt.x, dt.y);
float degree = rad * Mathf.Rad2Deg;
if (degree < 0)degree += 360;
return degree;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.