BubyMB
June 16, 2016, 10:51am
1
Hey guys! I juts wrote a script, It’s not working I do not know why. no errors or anything…
just a simple shoot script that shoots towards the player if they have the Hittable tag
using UnityEngine;
using System.Collections;
public class Attack : MonoBehaviour
{
public float fireRate = 3;
private float nextFire = 0.0F;
public int destroyTime = 1;
void Update()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag.Equals("Hittable"))
{
nextFire = Time.time + 2;
GameObject clone = PhotonNetwork.Instantiate("projectile", transform.position, transform.rotation, 0) as GameObject;
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = clone.GetComponent<Rigidbody>();
Temporary_RigidBody.AddForce(transform.forward * 1000);
clone.transform.LookAt(hit.collider.transform);
StartCoroutine(destroyTimer(clone));
}
}
}
}
IEnumerator destroyTimer(GameObject clone)
{
yield return new WaitForSeconds(destroyTime);
PhotonNetwork.Destroy(clone);
}
}
I was just reading over the code, I was thinking maybe it is because it is not on the same plane?
Does it execute everything ok but code after
if(hit.collider.tag.Equals(“Hittable”)){…}
Debug.DrawRay is your friend if you want to check what the ray is doing… as listener is hinting at, you need to put some Debug.logs in there to determine at what point it’s going wrong.
BubyMB
June 16, 2016, 2:18pm
4
Did that and it seems weird… It draws a super short line the same length each time
ah, transform.forward is “unit length” so the Debug.DrawRay is only going to be short, you can extend that (raycast takes its direction from the ray but without a given distance it defaults to infinity, drawray takes its direction and distance from the ray)…
BubyMB
June 16, 2016, 2:25pm
6
LeftyRighty:
ah, transform.forward is “unit length” so the Debug.DrawRay is only going to be short, you can extend that (raycast takes its direction from the ray but without a given distance it defaults to infinity, drawray takes its direction and distance from the ray)…
I don’t really understand what you are saying? how would I draw it so it goes along with the proper line?
Debug.DrawRay(transform.position, transform.forward * 10000);
or whatever arbitarily big number
you must have a maximum cast length in order for your cast to find anything. I had issues with this in the past also…
only if you are trying to use a layer parameter, they’re both numbers and thanks to how overloading works you can’t “skip” an optional parameter if they happen to be the same type.
BubyMB
June 17, 2016, 1:48am
10
Okay, I am using a working script now;
using UnityEngine;
using System.Collections;
public class Attack : MonoBehaviour
{
public float fireRate = 3;
private float nextFire = 0.0F;
public int destroyTime = 1;
void Update()
{
RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetKeyUp(KeyCode.Mouse0))
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Hittable" && Time.time > nextFire)
{
Debug.Log("Hit");
transform.LookAt(hit.collider.transform);
nextFire = Time.time + 2;
GameObject clone = PhotonNetwork.Instantiate("projectile", transform.position, transform.rotation, 0) as GameObject;
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = clone.GetComponent<Rigidbody>();
Temporary_RigidBody.AddForce(transform.forward * 1000);
clone.transform.LookAt(hit.collider.transform);
StartCoroutine(destroyTimer(clone));
}
}
}
IEnumerator destroyTimer(GameObject clone)
{
yield return new WaitForSeconds(destroyTime);
PhotonNetwork.Destroy(clone);
}
}
But now it doesn’t shoot the players only normal objects i.e decorations