当前位置:网站首页>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);
}
}边栏推荐
- The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
- Equipment simulation and deduction training system software
- Mujoco model learning record
- Database - MySQL advanced SQL statement (I)
- Domestic spot silver should be understood
- 2022 Heilongjiang latest fire protection facility operator simulation test question bank and answers
- Localization through custom services in the shuttle application
- Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
- Fix the problem that easycvr device video cannot be played
- Software construction scheme of smart factory collaborative management and control application system
猜你喜欢

This is the latest opportunity of the London bank trend

Source code of new campus errand / campus task platform on mutual station

From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl

Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"

Localization through custom services in the shuttle application

Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #

Work and leisure suggestions of old programmers

Set the style of QT property sheet control

Static timing analysis (STA) in ic/fpga design

The method of real-time tracking the current price of London Silver
随机推荐
MySQL + JSON = King fried
Batch export all pictures in PPT in one second
D. Yet Another Minimization Problem
Growing up in the competition -- (Guangyou's most handsome cub) Pikachu walking
Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
[CF559E]Gerald and Path
Opencv map reading test -- error resolution
Talk about the favorite tools used by project managers
An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press
L'ouverture d'un compte d'actions en ligne est - elle sécurisée? Fiable?
Calculation of intersection of two line segments
Mujoco XML modeling
Slider verification code identification gadget display
Sanfeng cloud 0215 I often use
Happy new year | 202112 monthly summary
golang中的select详解
[acnoi2022] color ball
Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
Three dimensional anti-terrorism Simulation Drill deduction training system software
Irradiance, Joule energy, exercise habits