Adding gun to our Robot
FIRST STEP: ADD GUN TO OUR PLAYER AND SHOOT WITH IT
Now the first thing we will do is create a script that will control our weapon, we will call it "Weapon" the script is below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public float offset;
public GameObject projectile;
public GameObject shotEffect;
public Transform shotPoint;
private float timeBtwShots;
public float startTimeBtwShots;
private void Update()
{
// Handles the weapon rotation
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
if (timeBtwShots <= 0)
{
if (Input.GetMouseButton(0))
{
Instantiate(shotEffect, shotPoint.position, Quaternion.identity);
Instantiate(projectile, shotPoint.position, transform.rotation);
timeBtwShots = startTimeBtwShots;
}
}
else
{
timeBtwShots -= Time.deltaTime;
}
}
}
After we write this script we will drag it at Gun gameObject |
After that, if you hit play, you will see that or gun faces our mouse cursor, like in the photo below, it looks cool, right?
Now is time for the bullet script and object
Firstly, let's write the bullet script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed;
public float lifeTime;
public float distance;
public int damage;
public LayerMask whatIsSolid;
public GameObject destroyEffect;
private void Start()
{
Invoke("DestroyProjectile", lifeTime);
}
private void Update()
{
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
if (hitInfo.collider != null)
{
if (hitInfo.collider.CompareTag("Enemy"))
{
}
DestroyProjectile();
}
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
void DestroyProjectile()
{
Instantiate(destroyEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
After we create the bullet script is time to create a bullet gameObject, this is how to do it
You will see a circle, color it purple like the gun and add to the circle, Bullet script, circle collider and rigidbody 2d, it should look like below
Now name the circle, bullet, and drag it to the project files, to make it prefab, and to remove it from scene
Now at JMO assets you have very cool particle system, add them at shoot effect to weapon script and destroy effect to bullet script, and will look smth like this
Now our player shoots with gun, now is time to add enemies, so that we have a target who to shoot, because shooting just in air is pointless
To add enemies, go to project ⇒ assets ⇒ enemy and there you will see enemy robot as a prefab
Click on the robot and drag it to the scene, now is time to write code for the enemy, also we should add a line of code to bullet script, this line of code will make our enemy take damage when it will get hit by the bullet
Firstly, we will create a script called "Enemy" and we will drag it to our enemy, code is below
using System.Collections; |
Add this script to EnemyRobot, and now our enemy with this script has a heath, and can die, but we also have to add that line of code at bullet script, to make it easier for you I will show you the full code, just copy and paste all the code to bullet script |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed;
public float lifeTime;
public float distance;
public int damage;
public LayerMask whatIsSolid;
public GameObject destroyEffect;
private void Start()
{
Invoke("DestroyProjectile", lifeTime);
}
private void Update()
{
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
if (hitInfo.collider != null)
{
if (hitInfo.collider.CompareTag("Enemy"))
{
hitInfo.collider.GetComponent<Enemy>().TakeDamage(damage);
}
DestroyProjectile();
}
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
void DestroyProjectile()
{
Instantiate(destroyEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Comments
Post a Comment