[Unity makes wheels] Realize a csgo-like weapon roulette function

foreword

Welcome to this article, today we will explore how to use Unity to make a feature - weapon roulette. Not only can this feature add a new dimension to your game, but it can also be used to spice up your sweepstakes. This article will guide you how to create a weapon roulette function similar to CS:GO.

Weapon roulette is a common and much-loved feature in games like CS:GO. It gives the player the chance to randomly choose one from a roulette wheel containing various precious weapons. This mechanism not only provides a sense of excitement and playability, but also helps players acquire rare and powerful weapons and equipment.

Our goal is to create a simple yet fun weapon roulette feature in Unity using some basic programming and design skills. In this article, you'll learn how to lay out the wheel, add optionals, implement spin effects, and handle the results of selections when the wheel stops.

Whether you're an experienced Unity developer or just starting out with game development, this article will provide you with helpful guidance and advice. Let's get started together and bring a whole new level of fun to your games or activities!
insert image description here
First look at the final effect of this article to decide whether you want to read it.
insert image description here
The source code is at the end of the article

material import

insert image description here

GUI PRO Kit - Sci-Fi, you can go to the asset store to buy:
https://assetstore.unity.com/packages/2d/gui/gui-pro-kit-sci-fi-194741

You can also download the one I prepared (free)
https://download.csdn.net/download/qq_36303853/88131958

start

1. Put the background and the circle in the middle, adjust the appropriate width, height and position

insert image description here

2. Add a selection image box

insert image description here
Rotation is controlled by controlling the value of the z axis
insert image description here
insert image description here

3. Add some weapon and prop options

insert image description here

4. Write the script RadialMenuManager

// 引入所需的库
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 定义一个名为RadialMenuManager的公共类,该类继承自MonoBehaviour
public class RadialMenuManager : MonoBehaviour
{
    
    
    // 定义公共变量
    public Transform center;
    public Transform selectobject;
    public GameObject RadialMenuRoot;
    bool isRadialMenuActive;

    // 在第一帧更新之前调用Start方法
    void Start()
    {
    
    
        // 初始化RadialMenu为非激活状态
        isRadialMenuActive = false;
        //默认关闭轮盘
        RadialMenuRoot.SetActive(false);
    }

    // 每一帧都会调用Update方法
    void Update()
    {
    
    
        // 检测是否按下了tab键
        if (Input.GetKeyDown(KeyCode.Tab))
        {
    
    
            // 切换RadialMenu的激活状态
            isRadialMenuActive = !isRadialMenuActive;
            if (isRadialMenuActive)
            {
    
    
                // 如果RadialMenu处于激活状态,则设置RadialMenuRoot为激活
                RadialMenuRoot.SetActive(true);
            }
            else
            {
    
    
                // 如果RadialMenu处于非激活状态,则设置RadialMenuRoot为非激活
                RadialMenuRoot.SetActive(false);
            }
        }

        // 如果RadialMenu处于激活状态
        if (isRadialMenuActive)
        {
    
    
            // 计算中心位置和鼠标位置的差值
            Vector2 delta = center.position - Input.mousePosition;
            // 计算角度
            float angle = Mathf.Atan2(delta.y, delta.x) * Mathf.Rad2Deg;
            angle += 180;
            for (int i = 0; i < 360; i += 45){
    
    
                if (angle > i && angle < i + 45)
                {
    
    
                    // 设置选中对象的旋转角度
                    selectobject.eulerAngles = new Vector3(0, 0, i);
                }
            }
        }
    }
}

5. Binding scripts and objects

insert image description here

6. Run the effect, press the tab key to open and close the roulette

insert image description here

7. Optimize and add the text of displaying the selected weapon

insert image description here
Supplementary script

using TMPro;

//文本组件
public TextMeshProUGUI HighlightedWeaponName;
//道具名称列表
public string[] InventoryWeaponNames;

...

int index = 0;
for (int i = 0; i < 360; i += 45){
    
    
    if (angle > i && angle < i + 45)
    {
    
    
        // 设置选中对象的旋转角度
        selectobject.eulerAngles = new Vector3(0, 0, i);
        //显示对应名称
        HighlightedWeaponName.text = InventoryWeaponNames[index];
    }
    index++;
}


insert image description here
Bind the text and configure the weapon name, note that the weapons are sorted counterclockwise
insert image description here

8. Add the effect of zooming in when the mouse is selected

supplementary code

//储存道具列表
public Transform[] itemSlots;

...

int index = 0;
for (int i = 0; i < 360; i += 45){
    
    
    if (angle > i && angle < i + 45)
    {
    
    
        // 设置选中对象的旋转角度
        selectobject.eulerAngles = new Vector3(0, 0, i);
        HighlightedWeaponName.text = InventoryWeaponNames[index];

        //设置只有选中的进行缩放
        foreach(Transform t in itemSlots){
    
    
            t.transform.localScale = new Vector3(1, 1, 1);
        }
        itemSlots[index].transform.localScale = new Vector3(1.3f, 1.3f, 1.3f);
    }
    index++;
}

Bind the props, remember the order is not wrong, I did not pay attention to the order of the props, so the props are all messed up, so here I re-adjust the order of the props
insert image description here

Effect
insert image description here

9. Add the animation effect of opening the roulette


insert image description here
Newly added animation, through the animation recording function, the final effect can be achieved sooninsert image description here

Source code download

https://download.csdn.net/download/qq_36303853/88137246

reference

【Video】https://www.youtube.com/watch?v=pmqs5zsKTTs

end

Gifts of roses, hand a fragrance! If the content of the article is helpful to you, please don't be stingy with yours 点赞评论和关注, so that I can receive feedback as soon as possible. Every time you write 支持is the biggest motivation for me to continue to create. Of course, if you find something in the article 存在错误or something 更好的解决方法, you are welcome to comment and private message me!

Well, I am 向宇, https://xiangyu.blog.csdn.net

A developer who worked silently in a small company, out of hobbies, recently began to study unity by himself. If you encounter any problems, you are also welcome to comment and private message me. Although I may not necessarily know some problems, I will check the information of all parties and try to give the best suggestions. I hope it can help more people who want to learn programming People, encourage each other~
insert image description here

Guess you like

Origin blog.csdn.net/qq_36303853/article/details/132012523