- Home /
Errors from raycast in C#
I tried to add accuracy to my raycast function in my gun script. Unfortunately i get these errors every time:
(53,16): error CS1502: The best overloaded method match for UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit, float, int)' has some invalid arguments (53,30): error CS1620: Argument
#2' is missing `out' modifier
Can anyone help me with this? Here is my script:
public int range = 10000;
public float spreadFactor = 0.02f;
public void fireShot(){
if (!am.IsPlaying (shoot))
{
Vector3 direction = transform.forward;
direction.x += Random.Range(-spreadFactor, spreadFactor);
direction.y += Random.Range(-spreadFactor, spreadFactor);
direction.z += Random.Range(-spreadFactor, spreadFactor);
am.CrossFadeQueued(shoot, 0.01f, QueueMode.PlayNow);
RaycastHit hit;
Ray ray = fpsCam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
if (Physics.Raycast (ray, direction, out hit, range))
{
if (hit.transform.tag == "Player")
{
hit.transform.GetComponent<PhotonView> ().RPC ("applyDamage", PhotonTargets.AllBuffered, damage);
}
GameObject particleClone;
particleClone = PhotonNetwork.Instantiate (hitPar.name, hit.point, Quaternion.LookRotation (hit.normal), 0) as GameObject;
Destroy (particleClone, 2);
Debug.Log (hit.transform.name);
}
}
Answer by jwulf · Jun 17, 2017 at 11:46 PM
Looking at your call, you give the method
A UnityEngine.Ray
A Vector3
A UnityEngine.RaycastHit (with out modifier)
An int
which appearantly does not match with what arguments the method UnityEngine.Physics.Raycast (or any of its overloads) expects. Your should check https://docs.unity3d.com/ScriptReference/Physics.Raycast.html for valid calls of the method. Roughly summarized, you can either use a UnityEngine.Ray OR two Vector3s (origin and direction) to describe the ray to be cast.
Thank you! This fixed my problem. I changed the origin from a ray to vector3 and it worked!
Your answer
![](https://koobas.hobune.stream/wayback/20220612124400im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to select an object with TOUCH and change its animation 2D 1 Answer
how to instantiate an object that is a child of another object 1 Answer
Change Transparency 1 Answer