2D Top-Down Shooting Script Errors
Hi guys, still on my way on creating a 2D top-down shooter game. This time I've somewhat made a rough script on my players Gun (shooting). However I'm currently getting a lot of error on two lines of codes. They aren't really using any Vector2's, I'm quite new to the whole 2D part of Unity I'm more comfortable in 3D. So if anyone can help me out on the errors that would great guys! Thank you so much, this community has helped me heaps :)
First error line: The first if function statement Second error line: The Debug.DrawRay
public class Gun : MonoBehaviour {
public enum GunType {Semi,Burst,Auto};
public GunType gunType;
public Transform spawn;
public void Shoot () {
Ray2D ray = new Ray2D (spawn.position, spawn.forward);
RaycastHit2D hit;
float shotDistance = 20;
if(Physics2D.Raycast(ray,out hit, shotDistance)) {
shotDistance = hit.distance;
}
Debug.DrawRay (ray.origin, ray.direction = shotDistance, Color.red, 1);
}
public void ShootContinuous () {
if (gunType == GunType.Auto) {
Shoot ();
}
}
}
Answer by TanselAltinel · Mar 29, 2016 at 12:24 PM
You are using
ray.direction = shotDistance
Which is false. You probably wanted to do a subtraction operation.
So write it as ray.direction - shotDistance ins$$anonymous$$d?
hmm I put a - ins$$anonymous$$d of the = sign but it's still giving the same error. Would you be able to help on the errors noted above?