轮转图绘制

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cyclogram : MonoBehaviour
{
    public GameObject prefb;
    public int num;
    //半径
    public  float r;
    List<GameObject>list=new List<GameObject>();//cun存所有球
    float angle;
    void Start()
    {
        angle = 2 * Mathf.PI / num;
        for (int i = 0; i < num; i++)
        {
            float x = Mathf.Sin(i * angle) * r;
            float z = Mathf.Cos(i * angle) * r;
            GameObject spher = Instantiate(prefb);
            spher.transform.parent = transform;
            spher.transform.localPosition = new Vector3(x, 0, z);
            spher.GetComponent<cyclogramitem>().cyclogram = this;
            list.Add(spher);    
        }
    }
    float allang = 0;
    public void OnDrag(float dis)
    {
        //
        float moveang = dis / r;//移动弧度
        allang-=moveang;//存的角度加移动的弧度  负号方向一致
        for (int i = 0; i < list.Count; i++)
        {
            float x = Mathf.Sin(i * angle+allang) * r;
            float z = Mathf.Cos(i * angle+ allang) * r;
            list[i].transform.localPosition = new Vector3(x, 0, z);
        }
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}

按住鼠标拖拽球形轮转 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cyclogramitem : MonoBehaviour
{
     public cyclogram cyclogram;
 
    private void OnMouseDrag()
    {
        Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
        Vector3 next = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, pos.z  )); 
        //距离-移动
       float dis=next.x-transform.position.x;
        cyclogram.OnDrag(dis);
    }

}

 添加手写dotween实现轮转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cyclogramitem : MonoBehaviour
{
     public cyclogram cyclogram;

  MouseDrag()
    {
        Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
        Vector3 next = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, pos.z  )); 
        //距离-移动
       float dis=next.x-transform.position.x;
        cyclogram.OnDrag(dis);
    }
    //鼠标抬起
    private void OnMouseUp()
    {
        Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
        Vector3 next = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, pos.z));
        //距离-移动
        float dis = next.x - transform.position.x;
        // cyclogram.OnDrag(dis);
        cyclogram.Inertia(dis); 
    }

}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DT : MonoBehaviour
{
    public Action<float> action;
    public float begin;
    public float end;
    public float time;
   
    float nowtime;
    public static DT To(Action<float> action,float begin,float end,float time)
    {
        GameObject dt = new GameObject("DT");
        DT dotween= dt.AddComponent<DT>();
        dotween.action = action;
        dotween.begin = begin;
        dotween.end = end;
        dotween.nowtime =Time.time;
      
        return dotween; 

    }
  

    // Update is called once per frame
    void Update()
    {
        if (Time.time-nowtime<time)//现在时间-初始化时间小于传的time
        {
            float t = Time.time - nowtime;
            if (t >time)
            {
                action(end);
                Destroy(gameObject);
            }
            else
            {
                float p = t / time;
                float a = begin *(1- p) + end * p;
                action(a);
            }
           
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cyclogram : MonoBehaviour
{
    public GameObject prefb;
    public int num;
    //半径
    public  float r;
    //减速度
    public float dec = 0.8f;
    List<GameObject>list=new List<GameObject>();//cun存所有球
    float angle;
    void Start()
    {
        angle = 2 * Mathf.PI / num;
        for (int i = 0; i < num; i++)
        {
            float x = Mathf.Sin(i * angle) * r;
            float z = Mathf.Cos(i * angle) * r;
            GameObject spher = Instantiate(prefb);
            spher.transform.parent = transform;
            spher.transform.localPosition = new Vector3(x, 0, z);
            spher.GetComponent<cyclogramitem>().cyclogram = this;
            list.Add(spher);    
        }
    }
    float allang = 0;
    public void OnDrag(float dis)
    {
        //
        float moveang = dis / r;//移动弧度
        allang-=moveang;//存的角度加移动的弧度  负号方向一致
        for (int i = 0; i < list.Count; i++)
        {
            float x = Mathf.Sin(i * angle+allang) * r;
            float z = Mathf.Cos(i * angle+ allang) * r;
            list[i].transform.localPosition = new Vector3(x, 0, z);
        }
    }

    public void Inertia(float dis)//惯性
    {
        float time =Mathf.Abs( dis / dec);
        DT.To((a) =>
        {
            OnDrag(a);   
        },dis,0,time);
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值