当前位置:网站首页>暑假学习记录
暑假学习记录
2022-06-30 10:48:00 【童萌依然】
代码完善
敌人代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed = 1f; //移动速度
Animator animator;
Rigidbody2D rigBody;
public Vector2 movePlay; //移动向量
public float moveTimer; //移动计时器
public float moveTime = 1; // 移动时间
bool IsMove = false;
Vector3 originPos;
Vector3 endPos;
float timers = 0;
int direction = 1;
public int Bloodvalue=50;
public SpriteRenderer rend;
public GameObject EnemyBullet;
public float BulletSpeed=2f;
public Transform EnemyTs;
IEnumerator TestIfTurn()
{
originPos = transform.position;
yield return new WaitForSeconds(0.05f);
endPos = transform.position;
if (Vector3.Distance(originPos, endPos) <= 0.001f)
{
direction = direction * (-1);
}
}
void Start()
{
rigBody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
rend = GetComponent<SpriteRenderer>();
//使用切换方向向量的函数
moveAuto();
}
// Update is called once per frame
void Update()
{
moveTimer -= Time.deltaTime;
//判断计时器完毕没有
if (moveTimer >= 2)
{
Vector3 thisScale = this.transform.localScale;
Vector2 position = rigBody.position;
position += movePlay * speed * Time.deltaTime;
rigBody.MovePosition(position); //调用刚体的位置赋值的方法
}
else if (moveTimer <= 2 && moveTimer >= 0)
{
animator.SetFloat("TransformX", 0);
animator.SetFloat("TransformY", 0);
}
else if (moveTimer <= 0)
{
//完毕了就重新调用,重新获取方向
moveAuto();
//计时器重新赋值
moveTimer = moveTime;
}
if (Bloodvalue <= 0)
{
Destroy(gameObject);
}
}
void moveAuto()
{
//随机生成-1,0,1的数字,因为随机数的Next不取最大值,此处用(-1,2)
//此处随机生成移动向量
System.Random rd = new System.Random();
float x = rd.Next(-1, 2);
float y = rd.Next(-1, 2);
movePlay = new Vector2(x, y);
//切换方向
Vector3 thisScale = this.transform.localScale;
if (x != 0)
{
thisScale.x = Math.Abs(thisScale.x) * x;
transform.localScale = thisScale;
}
GameObject Bullet = Instantiate(EnemyBullet,EnemyTs.position,Quaternion.identity)as GameObject;
Bullet.GetComponent<Rigidbody2D>().velocity = movePlay * BulletSpeed;
animator.SetFloat("TransformX", x);
animator.SetFloat("TransformY", y);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("bullet"))
{
rend.color = new Color(255, 255, 255, 122);
Debug.Log(other.name);
Blood(10);
Destroy(other.gameObject);
}
}
void Blood(int value)
{
Bloodvalue -= value;
}
}
玩家代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private static Player instance;
public static Player Instance {
get; private set; }
public int bloodvalue {
get; set; } = 7 ;
public int dunvalue {
get; set; } = 11;
public int magicvalue {
get; set; } = 200;
public float DunTime=5;
public float DunTimer;
public Transform hand;
private void Awake()
{
Instance = this;
}
public Animator anim;
public float runSpeed;
public bool run;
private float stopX,stopY;
private Rigidbody2D rigidbody;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
DunTimer -= Time.deltaTime;
if (DunTimer <= 0&&dunvalue<11)
{
dunvalue++;
}
if (bloodvalue >= 0)
{
Run();
}
if (bloodvalue <= 0)
{
bloodvalue = 0;
anim.SetBool("die", true);
}
}
void Run()
{
float moveDir = Input.GetAxis("Horizontal");
float moveDer = Input.GetAxis("Vertical");
Vector2 platerVel = new Vector2(moveDir * runSpeed, moveDer * runSpeed);
rigidbody.velocity = platerVel;
if (platerVel != Vector2.zero)
{
anim.SetBool("Run", true);
stopX = moveDir;
stopY = moveDer;
}
else
{
anim.SetBool("Run", false);
}
anim.SetFloat("InputX", stopX);
anim.SetFloat("InputY", stopY);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("EnemyBullet"))
{
DunTimer = DunTime;
Destroy(other.gameObject);
hurt(2);
}
if (other.CompareTag("weapon")&&Input.GetKeyDown(KeyCode.J))
{
other.transform.parent = hand;
other.transform.localPosition = Vector3.zero;
other.transform.localEulerAngles = new Vector3(-90, 0, 0);
}
}
void hurt(int attack)
{
if (dunvalue < 0)
{
dunvalue = 0;
bloodvalue -= attack;
}
else if (dunvalue > 0)
{
dunvalue -= attack;
}
}
}
UI

UI代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class UI : MonoBehaviour
{
public Slider PlayerBlood;
public Slider PlayerDun;
public Slider PlayerMagic;
public int maxblood = 7;
public int maxdun = 11;
public int maxmagic = 200;
public TextMeshProUGUI tmp;
public TextMeshProUGUI tmp1;
public TextMeshProUGUI tmp2;
public Button bt;
public GameObject gun;
public GameObject gun2;
public GameObject gunimage;
public GameObject gun2image;
public bool gunactive=true;
public bool gunactive2 = false;
// Start is called before the first frame update
void Start()
{
Player.Instance.bloodvalue = maxblood;
Player.Instance.dunvalue = maxdun;
Player.Instance.magicvalue = maxmagic;
}
// Update is called once per frame
void Update()
{
PlayerBlood.value = Player.Instance.bloodvalue;
PlayerDun.value = Player.Instance.dunvalue;
PlayerMagic.value = Player.Instance.magicvalue;
tmp.text = Player.Instance.bloodvalue + "" + "/" + maxblood + "";
tmp1.text = Player.Instance.dunvalue + "" + "/" + maxdun + "";
tmp2.text = Player.Instance.magicvalue + "" + "/" + maxmagic + "";
}
public void click()
{
gun.SetActive(!gunactive);
gun2.SetActive(!gunactive2);
gunimage.SetActive(!gunactive);
gun2image.SetActive(!gunactive2);
gunactive = !gunactive;
gunactive2 = !gunactive2;
}
}
边栏推荐
- Kotlin 协程调度切换线程是时候解开谜团了
- [proteus simulation] Arduino uno led simulated traffic light
- Pytorch notes: validation, model eval V.S torch. no_ grad
- Time complexity and space complexity
- 博弈论入门
- LVGL 8.2 re-coloring
- 再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
- 焕发青春的戴尔和苹果夹击,两大老牌PC企业极速衰败
- Collectors. Tomap application
- datax - 艰难debug路
猜你喜欢

The life, working principle and application of electrochemical oxygen sensor

Go language defer

OLAP数据库引擎如何选型?

时间复杂度与空间复杂度

中移OneOS开发板学习入门

China will force a unified charging interface. If Apple does not bow its head, iPhone will be kicked out of the Chinese market

第一届中国数字藏品大会即将召开

语音识别-基础(一):简介【语音转文本】

导致系统性能失败的10个原因

WireGuard简单配置
随机推荐
The jetpack compose dropdownmenu is displayed following the finger click position
Understanding society at the age of 14 - reading notes on "happiness at work"
SQL必需掌握的100个重要知识点:组合查询
SQL必需掌握的100个重要知识点:使用存储过程
Deep dive kotlin synergy (16): Channel
LVGL 8.2 Simple Colorwheel
AMS源码解析
SQL必需掌握的100个重要知识点:分组数据
200000 bonus pool! [Alibaba security × ICDM 2022] the risk commodity inspection competition on the large-scale e-commerce map is in hot registration
【西安交通大学】考研初试复试资料分享
经典面试题:负责的模块,针对这些功能点你是怎么设计测试用例的?【杭州多测师】【杭州多测师_王sir】...
The life, working principle and application of electrochemical oxygen sensor
华三交换机清空配置
我们公司使用 7 年的这套通用解决方案,打通了几十个系统,稳的一批!
语音信号处理-基础(五):傅立叶变换
LVGL 8.2 re-coloring
ESP32-C3入门教程 基础篇⑫——量产烧写设备配置和序列号, NVS partition分区确认, NVS 分区生成程序, csv转bin
Handler-源码分析
LeetCode Algorithm 86. 分隔链表
导致系统性能失败的10个原因