当前位置:网站首页>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;
}
}
边栏推荐
- The latest collection of arouter problems
- 暑假学习记录
- SQL必需掌握的100个重要知识点:更新和删除数据
- [applet practice series] Introduction to the registration life cycle of the applet framework page
- 数据库 自动增长
- QT embeds the sub QT program window into the current program
- 据说用了这个,老板连夜把测试开了
- SQL必需掌握的100个重要知识点:联结表
- 10天学会flutter DAY10 flutter 玩转 动画与打包
- AMS源码解析
猜你喜欢

Go language defer

HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴

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

The operation and maintenance security gateway (Fortress machine) of Qiming star group once again won the first place!

国内首批!阿里云云原生数据湖产品通过信通院评测认证

Is the golden cycle of domestic databases coming?

R语言去重操作unique duplicate filter

win10 R包安装报错:没有安装在arch=i386

8行代码实现快速排序,简单易懂图解!

关于IP定位查询接口的测评Ⅲ
随机推荐
100 important knowledge points that SQL must master: updating and deleting data
100 important knowledge points that SQL must master: summary data
DataX JSON description
Livedata source code appreciation III - frequently asked questions
[leetcode 16] sum of three numbers
[untitled]
训练一个图像分类器demo in PyTorch【学习笔记】
中移OneOS开发板学习入门
林克庆到番禺区调研“发展要安全”工作 以“时时放心不下”责任感抓好安全发展各项工作
The jetpack compose dropdownmenu is displayed following the finger click position
Esp32-c3 introductory tutorial question ⑨ - core 0 panic 'ed (load access fault) Exception was unhandled. vfprintf. c:1528
promise async和await的方法与使用
语音信号处理-基础(五):傅立叶变换
SQL必需掌握的100个重要知识点:组合查询
The reasoning delay on iphone12 is only 1.6 MS! Snap et al. Analyzed the transformer structure latency in detail, and used NAS to find out the efficient network structure of mobile devices
Line generation (Gauss elimination method, linear basis)
Introduction to China Mobile oneos development board
H3C switch emptying configuration
Alibaba cloud database represented by polardb ranks first in the world
Alibaba cloud lifeifei: China's cloud database has taken the lead in many mainstream technological innovations abroad