当前位置:网站首页>Summer vacation study record
Summer vacation study record
2022-06-30 11:31:00 【Tong Meng still】
A reprint of the vigorous knight
Code perfect
Enemy code
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed = 1f; // Movement speed
Animator animator;
Rigidbody2D rigBody;
public Vector2 movePlay; // Move the vector
public float moveTimer; // Move timer
public float moveTime = 1; // Move time
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>();
// Use a function that switches the direction vector
moveAuto();
}
// Update is called once per frame
void Update()
{
moveTimer -= Time.deltaTime;
// Judge whether the timer has finished
if (moveTimer >= 2)
{
Vector3 thisScale = this.transform.localScale;
Vector2 position = rigBody.position;
position += movePlay * speed * Time.deltaTime;
rigBody.MovePosition(position); // Call the method of rigid body position assignment
}
else if (moveTimer <= 2 && moveTimer >= 0)
{
animator.SetFloat("TransformX", 0);
animator.SetFloat("TransformY", 0);
}
else if (moveTimer <= 0)
{
// When finished, call again , Regain direction
moveAuto();
// The timer is reassigned
moveTimer = moveTime;
}
if (Bloodvalue <= 0)
{
Destroy(gameObject);
}
}
void moveAuto()
{
// Random generation -1,0,1 The number of , Because of the random number Next Do not take the maximum value , This is used here (-1,2)
// Here, a random motion vector is generated
System.Random rd = new System.Random();
float x = rd.Next(-1, 2);
float y = rd.Next(-1, 2);
movePlay = new Vector2(x, y);
// Switch direction
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;
}
}
Player code
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 Code
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;
}
}
边栏推荐
- Discussion on the essence of "FPGA mining" from open source projects
- Oceanbase installation Yum source configuration error and Solutions
- 再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
- 100 important knowledge points that SQL must master: using subquery
- Esp32-c3 introductory tutorial basic part ⑪ - reading and writing non-volatile storage (NVS) parameters
- 100 important knowledge points that SQL must master: Combined Query
- datax json说明
- ARouter 最新问题合集
- [xi'anjiaotonguniversity] information sharing of the first and second postgraduate entrance examinations
- 科普达人丨漫画图解什么是eRDMA?
猜你喜欢

国产数据库的黄金周期要来了吗?

Qualcomm released the "magic mirror" of the Internet of things case set, and digital agriculture has become a reality

以PolarDB为代表的阿里云数据库以跻身全球第一阵营

Wechat Emoji is written into the judgment, and every Emoji you send may become evidence in court

CVPR 2022 | 大幅减少零样本学习所需人工标注,马普所和北邮提出富含视觉信息的类别语义嵌入...

Is the golden cycle of domestic databases coming?

ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528

关于IP定位查询接口的测评Ⅲ

考研这些“不靠谱”的经验有多害人?

Le talent scientifique 丨 dessins animés qu'est - ce qu'erdma?
随机推荐
ESP32-C3入门教程 基础篇⑪——Non-Volatile Storage (NVS) 非易失性存储参数的读写
TypeScript ReadonlyArray(只读数组类型) 详细介绍
语音信号处理-基础(五):傅立叶变换
训练一个图像分类器demo in PyTorch【学习笔记】
100 important knowledge points that SQL must master: join table
再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
10 reasons for system performance failure
100 important knowledge points that SQL must master: using table aliases
据说用了这个,老板连夜把测试开了
揭秘得物客服IM全链路通信过程
Set up your own website (13)
Shutter from zero 004 button assembly
【IC5000教程】-01-使用daqIDEA图形化debug调试C代码
微信表情符号被写入判决书,你发的每个 emoji 都可能成为呈堂证供
SQL必需掌握的100个重要知识点:组合查询
Alibaba cloud lifeifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
Oracle NetSuite 助力 TCM Bio,洞悉数据变化,让业务发展更灵活
How harmful are these "unreliable" experiences in the postgraduate entrance examination?
数据库 自动增长
R语言查看版本 R包查看版本