当前位置:网站首页>How to change guns for 2D characters
How to change guns for 2D characters
2022-07-01 18:34:00 【A little dinosaur who can't code】
Changing guns and moving
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move: MonoBehaviour
{
public GameObject [] gun;// Store all guns
new private Rigidbody2D rigidbody;
private Vector2 input;// People move
private Vector2 mousePso;
public float Speed;
private int gunNum; // Record the gun number
private Animator animantor;
void Start()
{
animantor = GetComponent<Animator>();
rigidbody = GetComponent<Rigidbody2D>();
gun[0].SetActive(true);
}
void Update()
{
SwitchGun();
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
rigidbody.velocity = input.normalized * Speed;
mousePso = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (mousePso.x > transform.position.x)
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
}
else
{
transform.rotation = Quaternion.Euler(new Vector3(0, 180, 0));
}
if (input != Vector2.zero)
{
animantor.SetBool("Ismoving", true);
}
else
{
animantor.SetBool("Ismoving", false);
}
}
void SwitchGun()
{
if (Input.GetKeyDown(KeyCode.Q))
{
gun[gunNum].SetActive(false);
if (--gunNum < 0)
{
gunNum = gun.Length - 1;
}
gun[gunNum].SetActive(true);
}
if (Input.GetKeyDown(KeyCode.E))
{
gun[gunNum].SetActive(false);
if (++gunNum >gun.Length-1)
{
gunNum = 0;
}
gun[gunNum].SetActive(true);
}
}
}
Shooting
public class Gun : MonoBehaviour
{
public float interval;// Firing interval
public GameObject bulletPrefab;// The preform of a bullet
public GameObject shellPrefab;// Prefabrication of cartridge case
protected Transform shelllPos;// Magazine location
protected Transform bulletPos;// The location of the bullet
protected Animator animator;
protected Vector2 mousePos;// Mouse position
protected Vector2 direction;// The direction of the mouse
protected float timer;
protected float FlipY;
protected virtual void Start()
{
animator = GetComponent<Animator>();
bulletPos = transform.Find("Muzzle");
shelllPos = transform.Find("BulletShell");
FlipY = transform.localScale.y;
//Debug.Log(FlipY);
}
protected virtual void Update()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (mousePos.x < transform.position.x)
{
transform.localScale = new Vector3(FlipY, -FlipY, 1);
}
else
{
transform.localScale = new Vector3(FlipY, FlipY, 1);
}
Shoot();
}
protected virtual void Shoot()
{
direction = (mousePos - new Vector2(transform.position.x, transform.position.y)).normalized;
transform.right = direction;
if (timer != 0)
{
timer -= Time.deltaTime;
if (timer <= 0)
{
timer = 0;
}
}
if (Input.GetMouseButtonDown(0))
{
if (timer == 0)
{
timer = interval;
Fire();
}
}
}
protected virtual void Fire()
{
animator.SetTrigger("Shoot");
//GameObject bullet = Instantiate(bulletPrefab, bulletPos.position,Quaternion.identity);
GameObject bullet = objectPool.Instance.GetObject(bulletPrefab);
bullet.transform.position = bulletPos.position;
float angle = Random.Range(-5f, 5f);
bullet.GetComponent<bullet>().SetSpeed(Quaternion.AngleAxis(angle, Vector3.forward) * direction);
GameObject shell = objectPool.Instance.GetObject(shellPrefab);
shell.transform.position = shelllPos.position;
shell.transform.rotation = shelllPos.rotation;
// Instantiate(shellPrefab, shelllPos.position,shelllPos.rotation);
}
}
边栏推荐
- 聊聊项目经理最爱使用的工具
- 网上股票开户安全吗?是否可靠?
- Bug of QQ browser article comment: the commentator is wrong
- Data query language (DQL)
- Review Net 20th anniversary development and 51aspx growth
- When the fixed frequency artifact falls in love with multithreading | ros2 fixed frequency topic release demo
- Software construction scheme of smart factory collaborative management and control application system
- Distributed task queue: Celery usage record
- Mysql database design
- transform. Forward and vector3 Differences in the use of forward
猜你喜欢
Penetration practice vulnhub range Tornado
People help ant help task platform repair source code
Mysql database design
Oracle TRUNC function processing date format
Localization through custom services in the shuttle application
[PHP foundation] realize the connection between PHP and SQL database
Mujoco model learning record
12. Design of power divider for ads usage record
Calculation of intersection of two line segments
Blue Bridge Cup real topic: the shortest circuit
随机推荐
Yolov5 practice: teach object detection by hand
Three dimensional anti-terrorism Simulation Drill deduction training system software
[today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out
Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application
Is online stock account opening safe? Is it reliable?
Record 3 - the state machine realizes key control and measures the number of external pulses
Calculation of intersection of two line segments
Android development interview was badly hit in 3 years, and now the recruitment technical requirements are so high?
Slider verification code identification gadget display
The latest software scheme of the intelligent information management system of the armed police force
Distributed task queue: Celery usage record
Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
D. Yet Another Minimization Problem
LeetCode 148. Sort linked list
Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
Penetration practice vulnhub range Tornado
Happy new year | 202112 monthly summary
JS how to convert a string with a delimiter into an n-dimensional array
C operator overloads the query table
Talk about the favorite tools used by project managers