error CS1503: Argument `#1' cannot convert `UnityEngine.Vector3' expression to type `UnityEngine.Ray'
Hey everyone,
I don't even know what's going wrong here, the Unity database is super vague and I just don't know what I'm doing by using this, but I'm having a problem with Spherecast. I've tried searching for answers online, but I couldn't find an answer that solved my problem.
So this is the ONE line of code that has huge problems, I dont know if I need to supply more code, but if I do, I'd happily provide you with more of the code. Anyway, here goes:
Physics.SphereCast(gameObject.transform.position,.5f,transform.right)
I dont know what is going wrong. I dont know why this error is caused, when it seems like wherever I look, it tells me to give a vector3, which I am doing...
Some clarification or help would be great, thanks guys, Im just utterly lost on this one
Answer by Landern · Dec 19, 2016 at 05:10 PM
You're missing the RaycastHit.
RaycastHit hit;
Physics.SphereCast(gameObject.transform.position, 0.5f, transform.right, out hit);
The rest of the parameters after the RaycastHit have default parameters but you must supply origin, radius, direction, and hitinfo parameters.
Thank you very much, I've been able to fix my little issue :D
Answer by UnityCoach · Dec 19, 2016 at 05:16 PM
According to the documentation, there are three implementations :
public static bool SphereCast(Vector3 origin, float radius, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
public static bool SphereCast(Ray ray, float radius, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
public static bool SphereCast(Ray ray, float radius, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
The first one is the only one to take a Vector3. So you need to at least pass it : (Vector3 origin, float radius, Vector3 direction, out RaycastHit hitInfo)
RaycastHit hitInfo;
Physics.SphereCast(gameObject.transform.position,.5f,transform.right, out hitInfo);
Answer by LK84 · Dec 19, 2016 at 05:16 PM
There are three ways to overload Physics.SphereCast https://docs.unity3d.com/ScriptReference/Physics.SphereCast.html If you wanna use parameters Vector3, float, Vector3 you have to add at least one more parameter of type RaycastHit with the 'out' keyword. Your method call could look like this:
RayCastHit RCInfo;
Physics.SphereCast(gameObject.transform.position,.5f,transform.right, out RCInfo)
Note: You get the conversion error because when the compiler notices that you entered three parameters, he will try to use on of the other overload methods where he is expecting a variable of type 'Ray'
Thanks, turned out I should've paid a bit more attention.