Unity2D 旋转转盘(按左键拖动旋转)

这里讲的方法不是抽奖的那种随机转盘 注意

这里讲的方法是按住鼠标左键来转动转盘
在这里插入图片描述

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

[RequireComponent(typeof(AudioSource))]
public class Rotate : MonoBehaviour
{
// Start is called before the first frame update

private bool isTouch = false;
private AudioSource Player;
public float speed = 100f;
public AudioClip select;
private GameObject aud;

void Start()
{
    this.Player = this.GetComponent<AudioSource>();
}

// Update is called once per frame
void Update()
{
    aud = GameObject.Find("One shot audio");
    if (Input.GetMouseButton(0) && isTouch)
    {
        AudioPlay(select);
        transform.Rotate(0, 0, -Input.GetAxis("Mouse X") * Time.deltaTime * speed);
    }

    if (Input.GetMouseButtonUp(0))
    {
        isTouch = false;
        StopAudio();
    }

    if(Input.touchCount > 0)
    {

        Vector3 position;
        RaycastHit2D hit;

        Touch touch = Input.GetTouch(0);

        position = Camera.main.ScreenToWorldPoint(touch.position);

        hit = Physics2D.Raycast(position, Vector2.zero);


        if (hit.collider != null)
        {
            if(hit.collider.gameObject == gameObject)
            {
                if (touch.phase == TouchPhase.Began)
                {
                    
                    isTouch = true;
                }
                else if (touch.phase == TouchPhase.Moved)
                {
                    GetComponent<Rigidbody2D>().AddTorque(-touch.deltaPosition.x * Time.deltaTime * speed); //transform.Rotate(0, 0, -touch.position.x * Time.deltaTime * speed);
                    AudioPlay(select);
                }
                else if (touch.phase == TouchPhase.Ended)
                {
                    isTouch = false;
                }

            }
        }
    }
}

public void OnMouseDown()
{
    isTouch = true;
}
public void AudioPlay(AudioClip clip)
{
    if (aud == null)
    {
        AudioSource.PlayClipAtPoint(clip,this.transform.position);
    }
}
//停止播放声音
public void StopAudio()
{
    Destroy(aud);
}

}

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读