- Home /
Raycasting issues raycast angled and not hitting
I created a script with the help of the users on this post http://answers.unity3d.com/questions/663862/c-weapon-shooting-script-using-raycasting-help.html I thought everything was working but when I moved the object a bit it would not hit. I decided to make the raycast show up in scene view and it seems to be firing on a 45 degree angle to the right of the gun. As shown in the image below. I also set the layer on the gun to "Ignore Raycast".
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public bool outofammo;
public int bullets = 7;
public GameObject deagle; //this is my gun
public float time = 1;
public float firerate = 1;
public bool fired = false;
public bool Cannotfire = false;
public bool timedown;
Transform Effect;
public float TakeDamage = 100;
RaycastHit hit;
private Vector3 lineTransform;
private Vector3 startTransform;
public int Damage = 25;
public float Distance;
public float MaxDistance = 2;
void Start()
{
lineTransform = transform.position;
startTransform = transform.position;
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.R) && bullets < 7)
{
deagle.animation.Play ("Reload");
bullets = 7;
}
if (Input.GetButtonDown("Fire1"))
{
timedown = true;
if (bullets == 0)
{
bullets = 7;
outofammo = true;
timedown = false;
}
else
outofammo = false;
if (outofammo == true)
{
deagle.animation.Play ("Reload");
}
}
if (timedown == true)
{
time = (time - (firerate *Time.deltaTime));
if (time <= 0)
{
bullets = bullets -1;
fired = true;
{
RaycastHit hit;
if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), out hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
hit.transform.SendMessage ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
lineTransform = hit.point;
Debug.DrawRay (startTransform, lineTransform, Color.red);
}
timedown = false;
time = 1f;
fired = false;
}
}
if (fired == true)
time = 1;
}
}
}
Not only that but I don't think the raycast is actually registering the hit either because I have this script attached to the object that it is hitting and nothing seems to be happening.
using UnityEngine;
using System.Collections;
public class TakeDamage2 : MonoBehaviour
{
public float health = 100;
public GameObject Enemy;
void ApplyDamage(int damage)
{
health -= damage;
}
}
Sorry for having to post about the same issue I thought it was all working. :/
Answer by robertbu · Mar 17, 2014 at 07:30 AM
The issue with the Debug.DrawRay() is that you are passing it two points. Debug.DrawRay() takes a point and a direction (like you use in your Raycast()). So you can do one of two things. You can change your DrawRay() to a DrawLine():
Debug.DrawLine(transform.position, hit.point);
Or you can rework your DrawRay():
Debug.DrawRay (transform.position, transform.forward * 10.0f, Color.red);
Ah I see why now, the gun was originally facing downwards which I had to fix by rotating it. So my "Vector3.forward" is actually downwards so it was shooting the ground. Thank you so much this confused me so much, I checked and it actually does register the hit now. The line was just wrong. Thank you so much this confused me so much. To think it was as simple as a rotation.
I've converted it to an answer. You can click on the checkmark to mark it solved. I originally did not put it in as an answer because it did not address this part of the problem:
but when I moved the object a bit it would not hit.