当前位置:网站首页>Summer vacation study record
Summer vacation study record
2022-06-30 11:27:00 【Tong Meng still】
Try 2D Game vitality Knight
1. Map making

By means of windows=>2D=>Tilemap Put the desired map in this box , You can click the brush to add a map to the world map . And you need to create the game object first Tilemap.
Add the required game objects to the map .
2. The enemy made
Animate the enemy , And make corresponding animation state machine control .

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;
// Must be in FixedUpdate in
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>();
// 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;
}
}
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;
}
animator.SetFloat("TransformX", x);
animator.SetFloat("TransformY", y);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Bullet"))
{
Destroy(other.gameObject);
}
}
void Blood(int value)
{
Bloodvalue -= value;
}
}
3. Protagonist production
It is similar to the enemy , Make animation first , Adding animation to the animation state machine 
The code is as follows
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
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()
{
Run();
}
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);
}
}
Gun code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
{
public GameObject Prefab;
public Transform Shoottransform;
public GameObject Player;
private float m_nextFire;
/// <summary>
/// Firing rate
/// </summary>
public float FireRate = 2.0f;
/// <summary>
/// The target of the bullet
/// </summary>
public GameObject Bullet;
/// <summary>
/// Bullet speed
/// </summary>
public float BulletSpeed;
void FixedUpdate()
{
Shoot();
// Get mouse coordinates and convert them to world coordinates
Vector3 m_mousePosition = Input.mousePosition;
m_mousePosition = Camera.main.ScreenToWorldPoint(m_mousePosition);
// the reason being that 2D, In less than z Axis . Make z The value of the axis is 0, So the coordinates of the mouse are (x,y) On the plane
m_mousePosition.z = 0;
// Weapon orientation angle
float m_weaponAngle = Vector2.Angle(m_mousePosition - this.transform.position, Vector2.right);
if (transform.position.x < m_mousePosition.x)
{
m_weaponAngle = -m_weaponAngle;
}
// Judge the pros and cons of weapons
if ( transform.localScale.x > 0)
{
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
}
else if ( transform.localScale.x < 0)
{
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
}
// Change the final angle
transform.eulerAngles = new Vector3(0, 0, m_weaponAngle);
}
void Shoot()
{
m_nextFire = m_nextFire + Time.fixedDeltaTime;
if(Input.GetMouseButton(0) && m_nextFire>FireRate)
{
// Get mouse coordinates and convert them to world coordinates
Vector3 m_mousePosition = Input.mousePosition;
m_mousePosition = Camera.main.ScreenToWorldPoint(m_mousePosition);
// the reason being that 2D, In less than z Axis . Make z The value of the axis is 0, So the coordinates of the mouse are (x,y) On the plane
m_mousePosition.z = 0;
// Bullet angle
float m_fireAngle = Vector2.Angle(m_mousePosition - this.transform.position, Vector2.up);
if(m_mousePosition.x>this.transform.position.x)
{
m_fireAngle = -m_fireAngle;
}
// Zero the timer
m_nextFire = 0;
// Generate bullets
GameObject m_bullet = Instantiate(Bullet, transform.position, Quaternion.identity) as GameObject;
// Speed
m_bullet.GetComponent<Rigidbody2D>().velocity = ((m_mousePosition-transform.position).normalized * BulletSpeed);
// angle
m_bullet.transform.eulerAngles = new Vector3(0, 0, m_fireAngle);
}
}
}
Camera follow code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainCamera : MonoBehaviour
{
public Transform target;
public float smoothing;
internal static readonly object main;
// Define two positions to limit camera movement
// Start is called before the first frame update
void Start()
{
}
void Update()
{
}
void LateUpdate()
{
if (target != null)
{
Vector3 targetPos = target.position;
transform.position = Vector3.Lerp(transform.position, targetPos, smoothing);
}
}
}
边栏推荐
- 【西安交通大学】考研初试复试资料分享
- The precision problem of depth texture in unity shader - stepping pit - BRP pipeline (there is no solution, it is recommended to replace URP)
- Qt嵌入子Qt程序窗口到当前程序
- SQL必需掌握的100个重要知识点:更新和删除数据
- SQL必需掌握的100个重要知识点:联结表
- LVGL 8.2 menu from a drop-down list
- 10天学会flutter DAY10 flutter 玩转 动画与打包
- LVGL 8.2 Simple Colorwheel
- Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change
- EMC-浪涌
猜你喜欢
随机推荐
LVGL 8.2 Drop down in four directions
AMS source code analysis
ESP32-C3入门教程 IoT篇⑤——阿里云 物联网平台 EspAliYun RGB LED 实战之批量生产的解决方案
Livedata source code appreciation III - frequently asked questions
压缩状态DP位运算
[leetcode 16] sum of three numbers
Qt嵌入子Qt程序窗口到当前程序
Record the memory leak of viewpager + recyclerview once
Problems and solutions in pyinstall packaging for pychart project
SQL必需掌握的100个重要知识点:使用子查询
Qualcomm released the "magic mirror" of the Internet of things case set, and digital agriculture has become a reality
什么是微信小程序,带你推开小程序的大门
LVGL8.2 Simple Checkboxes
100 important knowledge points that SQL must master: summary data
LeetCode Algorithm 86. 分隔鏈錶
LED driver library based on Hal Library
SQL必需掌握的100个重要知识点:汇总数据
[xi'anjiaotonguniversity] information sharing of the first and second postgraduate entrance examinations
Create - configure factory
Esp32-c3 introductory tutorial basic part ⑪ - reading and writing non-volatile storage (NVS) parameters









