using UnityEngine;
using System.Collections;
using System;
public class Weapon : MonoBehaviour {
public float fireRate = 0;
public float Damage = 10;
public LayerMask whatToHit;
public GameObject playerObject;
private bool m_FacingRight = true;
public Transform BulletTrailPrefab;
public Transform MuzzleFlashprefab;
float timetoSpawnEffect = 0;
public float effectSpawnRate = 10;
private float move;
float timeToFire = 0;
public Transform firePoint;
// Use this for initialization
void Awake() {
Transform firePoint = transform.FindChild("firePoint");
if (firePoint == null) {
Debug.LogError("No FirePoint? what?!?");
{
}
}
}
// Update is called once per frame
void Update() {
if (fireRate == 0) {
if (Input.GetButtonDown("Fire1")) {
Shoot();
}
}
else {
if (Input.GetButton("Fire1") && Time.time > timeToFire) {
timeToFire = Time.time + 1 / fireRate;
Shoot();
if (move < 0)
{
m_FacingRight = false;
transform.localRotation = Quaternion.Euler(0, 180, 0);
}
else
{
m_FacingRight = true;
transform.localRotation = Quaternion.Euler(0, 0, 0);
}
}
}
}
private void Shoot()
{
Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast(firePointPosition, mousePosition - firePointPosition, 100, whatToHit);
if (Time.time >= timetoSpawnEffect)
{
Effect();
timetoSpawnEffect = Time.time + 1 / effectSpawnRate;
}
Debug.DrawLine(firePointPosition, (mousePosition - firePointPosition) * 100, Color.cyan);
if (hit.collider != null)
{
Debug.DrawLine(firePointPosition, hit.point, Color.red);
Debug.Log("We hit " + hit.collider.name + " and did " + Damage + " damage.");
}
}
private void Effect() {
Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation);
Transform Clone = Instantiate (MuzzleFlashprefab, firePoint.position, firePoint.rotation) as Transform;
Clone.parent = firePoint;
float size = UnityEngine.Random.Range(0.7f, 0.9f);
Clone.localScale = new Vector3 (size, size, size);
Destroy (Clone.gameObject, 0.2f);
}
}
So it all works but it rarely does damage to actually what my mouse and the raycast is pointing at.
See if this makes things a little clearer as to whats happening:
private void Shoot() {
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 shootDirection = (mousePosition - (Vector2)firePoint.position).normalized;
Ray2D ray = new Ray2D(firePoint.position, shootDirection);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 100, whatToHit);
if(hit.collider != null) {
Debug.DrawLine(ray.origin, hit.point, Color.green);
Debug.Log("We hit " + hit.collider.name + " and did " + Damage + " damage.");
} else {
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
}
if(Time.time >= timetoSpawnEffect) {
Effect();
timetoSpawnEffect = Time.time + 1 / effectSpawnRate;
}
}
LiterallyJeff:
See if this makes things a little clearer as to whats happening:
private void Shoot() {
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 shootDirection = (mousePosition - (Vector2)firePoint.position).normalized;
Ray2D ray = new Ray2D(firePoint.position, shootDirection);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 100, whatToHit);
if(hit.collider != null) {
Debug.DrawLine(ray.origin, hit.point, Color.green);
Debug.Log("We hit " + hit.collider.name + " and did " + Damage + " damage.");
} else {
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
}
if(Time.time >= timetoSpawnEffect) {
Effect();
timetoSpawnEffect = Time.time + 1 / effectSpawnRate;
}
}
What ever way I face, and say i’m shooting at an enemy it doesn’t matter what way i’m facing as it still damages the enemy.
Where is the code that damages the enemy?
It just tells you what the damage is whatever sprite your hitting, e.g. building or an enemy at the moment.
Oh so what you’re saying is that it always outputs that you’re hitting the enemy even when you click elsewhere? What do you see from the Debug Drawline? Does it go towards the mouse and/or turn green? (assuming you’re using that debug code i wrote)
Perhaps try using if(hit) instead of if(hit.collider != null), it’s possible that always comes back not null.
LiterallyJeff:
Oh so what you’re saying is that it always outputs that you’re hitting the enemy even when you click elsewhere? What do you see from the Debug Drawline? Does it go towards the mouse and/or turn green? (assuming you’re using that debug code i wrote)
Perhaps try using if(hit) instead of if(hit.collider != null), it’s possible that always comes back not null.
The Debug Drawline is the opposite to where I face it with my cursor and only if its quick and easy to fix, how can I fix this? http://prntscr.com/d62lby
Lets handle one thing at a time.
You are using my code, correct? The DrawRay and DrawLine are using the same origin and direction, so I don’t know why it would be any different.
Either way, your shot appears to be going the correct direction.
Do all the shots hit the same object or are they different?
LiterallyJeff:
Lets handle one thing at a time.
You are using my code, correct? The DrawRay and DrawLine are using the same origin and direction, so I don’t know why it would be any different.
Either way, your shot appears to be going the correct direction.
Do all the shots hit the same object or are they different?
They hit different objects.
I’m a little confused, just earlier you said that it always hits the enemy:
It should be outputting whatever the raycast is hitting, even if the raycast is going backwards. The DrawLine function should be rendering exactly where the raycast is going. Is it outputting what the line is hitting?
LiterallyJeff:
I’m a little confused, just earlier you said that it always hits the enemy:
It should be outputting whatever the raycast is hitting, even if the raycast is going backwards. The DrawLine function should be rendering exactly where the raycast is going. Is it outputting what the line is hitting?
I used an enemy as an example.
I’m as well watching Brackeys tutorials.