爱心型血条

准备一张红色爱心及灰色爱心的图片

hearts添加布局

hg为灰色爱心,hr为红色爱心

hearts添加代码

hearts.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class hearts : MonoBehaviour {
    public Transform heart0;
    public Transform heart1;
    public Transform heart2;
    private List<Transform> heartList;
    private int maxHeartNum = 3;
    private int heartNum = 3;
    void Start () {
        heartList=new List<Transform>();
        heartList.Add(heart0);
        heartList.Add(heart1);
        heartList.Add(heart2);
    }
    void Update () {
        if (Input.GetKeyDown (KeyCode.A)) {
            if (heartNum > 0) {
                heartNum -= 1;
                heartList[heartNum].GetChild(1).GetComponent<Image>().enabled=false;
            }
        }
        if (Input.GetKeyDown (KeyCode.D)) {
            if (heartNum <maxHeartNum) {
                heartList[heartNum].GetChild(1).GetComponent<Image>().enabled=true;
                heartNum += 1;
            }
        }
    }
}

绑定对象

效果

2d横版主角移动(刚体)

https://www.bilibili.com/video/av82865782?t=818

随便布置一个主角和两个地

给主角和地面添加Collider,给主角添加Rigidbody

主角添加脚本

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

using UnityEngine;

public class Player : MonoBehaviour {
    public Rigidbody2D rb;
    public Collider2D coll;
    public float jumpforce, speed;

    public Transform groundCheck;
    public LayerMask ground; //碰撞体过滤 

    public bool isGround, isJump;

    bool jumpPressed;
    int jumpCount;
    void Start () {
        rb = GetComponent<Rigidbody2D> ();
        coll = GetComponent<Collider2D> ();
    }
    void Update () {
        if (Input.GetButtonDown ("Jump") && jumpCount > 0) {
            jumpPressed = true;
        }

    }

    //每秒固定执行的操作,与设备无关
    private void FixedUpdate () {
        isGround = Physics2D.OverlapCircle (groundCheck.position, 0.1f, ground); //判断是否在地面
        GroundMovement ();
        //SwitchAnim ();
        Jump ();
    }

    //根据输入控制水平方向的移动和以及方向变化
    void GroundMovement () {
        float horizontalMove = Input.GetAxisRaw ("Horizontal"); //只有0 -1 1 三个数字
        rb.velocity = new Vector2 (horizontalMove * speed, rb.velocity.y);

        if (horizontalMove != 0) {
            transform.localScale = new Vector3 (horizontalMove, 1, 1); //控制翻转
        }

    }

    void Jump () {

        if (isGround) {
            jumpCount = 2; //在地面的时候就恢复可跳跃次数
            isJump = false;
        }

        if (jumpPressed && isGround) {
            isJump = true;
            rb.velocity = new Vector2 (rb.velocity.x, jumpforce);
            jumpCount--;
            jumpPressed = false; //每0.02秒检测按键是否松开

        }
        else if (jumpPressed && jumpCount > 0 && !isGround) {
            rb.velocity = new Vector2 (rb.velocity.x, jumpforce);
            jumpCount--;
            jumpPressed = false;

        }

    }
}

将地面设为land图层

为了防止主角因为碰撞导致旋转,可以勾选

效果

主角切换形态

根据上面的主角制作不同的4个prefab

适当修改collider的形状以及groundCheck的位置

放在Assets>Resources>Prefabs路径下

Player.cs中修改脚本

按下字母区上方数字键1234就会切换不同的形态

void Update () {
    if (Input.GetButtonDown ("Jump") && jumpCount > 0) {
        jumpPressed = true;
    }
    if (Input.GetKeyDown (KeyCode.Alpha1)) {
        GameObject objPrefab = (GameObject) Resources.Load ("Prefabs/playerCat");
        Destroy (gameObject);
        Instantiate (objPrefab, transform.position, transform.rotation);
    }
    else
        if (Input.GetKeyDown (KeyCode.Alpha2)) {
            GameObject objPrefab = (GameObject) Resources.Load ("Prefabs/playerPanda");
            Destroy (gameObject);
            Instantiate (objPrefab, transform.position, transform.rotation);
        }
    else
        if (Input.GetKeyDown (KeyCode.Alpha3)) {
            GameObject objPrefab = (GameObject) Resources.Load ("Prefabs/playerPig");
            Destroy (gameObject);
            Instantiate (objPrefab, transform.position, transform.rotation);
        }
    else
        if (Input.GetKeyDown (KeyCode.Alpha4)) {
            GameObject objPrefab = (GameObject) Resources.Load ("Prefabs/playerRabbit");
            Destroy (gameObject);
            Instantiate (objPrefab, transform.position, transform.rotation);
        }

}

效果

轮盘选择1

五个Image

  • Image

    • Image
    • Image
    • Image
    • Image

排成两排两列,按顺时针分别编号0,1,2,3

将四个图像的透明度都调成150

Whell添加脚本

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

using UnityEngine;
using UnityEngine.UI;

public class Whell : MonoBehaviour {
    public Transform cat;
    public Transform panda;

    public Transform pig;
    public Transform rabbit;

    private int chooseNum;
    private Vector3 startPosition;
    void Start () {
        chooseNum = 1;
    }
    void Update () {
        if(Input.GetKeyDown(KeyCode.LeftAlt)){
            startPosition = Input.mousePosition;
        }
        if (Input.GetKey (KeyCode.LeftAlt)) {
            float angle = Vector2.SignedAngle (new Vector3 (1, 0, 0), Input.mousePosition - startPosition);
            if (angle > 90 && angle < 180) {
                chooseNum = 0;
            }
            else if (angle > 0 && angle < 90) {
                chooseNum = 1;
            }
            else if (angle < 0 && angle > -90) {
                chooseNum = 2;
            }
            else if (angle < -90 && angle > -180) {
                chooseNum = 3;
            }
            Color color = new Color (150, 150, 150, 1);
            transform.GetChild (chooseNum).GetComponent<Image> ().color = color;
            for (int i = 0; i < transform.childCount; i++) {
                if (i != chooseNum) {
                    transform.GetChild (i).GetComponent<Image> ().color = new Color (150, 150, 150, 125/255f);
                }
            }
        }
        if (Input.GetKeyUp (KeyCode.LeftAlt)) {
            for (int i = 0; i < transform.childCount; i++) {
                transform.GetChild (i).GetComponent<Image> ().color = new Color (150, 150, 150, 125/255f);
            }
        }
    }
}

原理是记录初始鼠标坐标,每次移动划一个向量,使用SignedAngle方法求该向量与x方向单位向量的角度

绑定属性

效果

轮盘选择2

现在将主角切换与轮盘结合起来

将轮盘做成预制体,放到该目录下

Player.cs中添加

void Update () {
    if(Input.GetKeyDown(KeyCode.LeftAlt)){
        GameObject whellPrefab = (GameObject) Resources.Load ("Prefabs/Whell");
        GameObject whell= Instantiate(whellPrefab);
        whell.transform.SetParent(GameObject.Find("Canvas").transform,false);
    }
}

再在Whell.cs中修改

void Start () {
      startPosition = Input.mousePosition;
}

效果

Last modification:October 1st, 2020 at 10:53 am