Skip to content

Instantly share code, notes, and snippets.

@tubakihimeLoveHate
Created November 4, 2019 13:00
Show Gist options
  • Save tubakihimeLoveHate/2523c951bc51f9d6352d12904b9b76ab to your computer and use it in GitHub Desktop.
Save tubakihimeLoveHate/2523c951bc51f9d6352d12904b9b76ab to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ControllerUI : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
{
    [SerializeField]//基礎スピード
    float speed = 3f;
    //歩行時=1,走行時=2
    float pulusSpeed=1;
private float maxSpeed = 2;
private float minSpeed = 1;
    Vector2 startPosition;
    Vector2 endPosition;
    [SerializeField]//コントローラーが動ける範囲
    float radius=50;
    bool isMove = false;
    [SerializeField]//動かしたいオブジェクトを選択
    Transform charactor;
    //ラジアン
    private float deg = 0;
    //回転間隔
    float deltaTime = 0.1f;
[SerializeField]
Animator animator;
[SerializeField]
Transform controllerBackPosition;
[SerializeField]
Transform controllerPosition;
float distance;
    // Start is called before the first frame update
    void Start()
    {
//最初は非表示に
controllerBackPosition.gameObject.SetActive(false);
controllerPosition.gameObject.SetActive(false);
    }
    // Update is called once per frame
    void Update()
    {
       if(isMove){
            Vector3 velocity = new Vector3(0, 0, 1);
            // キャラクターのローカル空間での方向に変換
            velocity = charactor.TransformDirection(velocity);
float speedCache = speed *pulusSpeed* Time.deltaTime;
            // キャラクターの移動
            charactor.localPosition += velocity * speedCache;
//アニメーション
animator.SetBool("walking",true);
if(distance > 40){
if(pulusSpeed<2)pulusSpeed += 0.1f;
else pulusSpeed = 2;
}else{
if(pulusSpeed>1)pulusSpeed -= 0.1f;
else pulusSpeed = 1;
}
animator.SetFloat("walk_runBlend",(pulusSpeed-minSpeed)/(maxSpeed-minSpeed));
        }else{
animator.SetBool("walking",false);
}
    }
    // ドラックが開始したとき
    public void OnBeginDrag(PointerEventData eventData)
    {
//変更したところ
        startPosition = new Vector2(0,0);
Vector3 local = transform.InverseTransformPoint (eventData.position);
controllerBackPosition.gameObject.SetActive(true);
controllerPosition.gameObject.SetActive(true);
controllerBackPosition.localPosition = local;
controllerPosition.localPosition = new Vector3(0,0,0);
        isMove = true;
    }
    // ドラック中
    public void OnDrag(PointerEventData eventData)
    {
Vector2 controllerLocal = controllerBackPosition.InverseTransformPoint(eventData.position);
        //コントローラーを円から出さない用制御+speed調節
        distance = Vector2.Distance(startPosition,controllerLocal);
        if(distance<radius){
//transform.position = eventData.position;
controllerPosition.localPosition = controllerLocal;
}else{
distance=radius;
}
        
     //角度算出
        deg = GetAngle(startPosition,controllerLocal);
//方向変換
        charactor.eulerAngles = new Vector3(0,deg,0);
   
    }
    // ドラックが終了したとき
    public void OnEndDrag(PointerEventData eventData)
    {
        transform.localPosition = new Vector2(0,0);
distance =0;
        isMove =  false;
controllerBackPosition.gameObject.SetActive(false);
controllerPosition.gameObject.SetActive(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