- Home /
Raycast Ray ray origin problem
Hi there! I'm having a problem calling a Raycast to handle projectiles.
I type Physics.Raycast(Ray ray,out RayCastHit hitInfo, float Distance) and then a problem occurs.
What I'm trying to do with my Raycast, is to originate it from my player position.
So I state that Ray ray = new Vector3 ( my player's x,y,z).
That line gives Error CS0029: cannot implicitly convert type " UnityEngine.Vector3" to "UnityEngine.Ray".
Now, I thought Ray ray was supposed to be a Vector3, so what's wrong?
Please, since Raycasts aren't that easy and I'm just a beginner, try to be as clear as possible in your answers... Thanks for the patience!
EDIT: My Script now is this
if(Input.GetKeyDown(KeyCode.Space)){
RaycastHit hitInfo;
if(Physics.Raycast(transform.position, transform.forward,out hitInfo, rayDistance)){
Debug.Log (" Raycast Doing Good");
}
}
It's working, and it's stored in the Update Function of my Player' Script.
I'd like to know how to get the same result when I Raycast from another script and I still want to origin my ray from my Player GameObject position.
I tried to store my player position in static variables to call them with Player.myPlayerPositionX and the likes, with no success at all.
IF I declare a new Public Static originRay(myPlayerPosition) where myPlayerPosition is a Vector3 I can then place the Raycast in the update of another script not related direcly with my Player GameObject and call this originRay in Ray ray = originRay... if that's right I've got my answer.
Thanks for the feedback!
Answer by Hoeloe · Sep 29, 2013 at 05:26 PM
Ray is not type Vector3, it is type Ray. Types are not so fickle as that - if it says it's type "Ray" (which you defined it as being when you wrote Ray ray
), then it is type Ray, not type Vector3. The only time something like you've written is valid (i.e. T1 t = new T2()
) is when T2 is a subclass of T2. Since Vector3 is a struct, it cannot be a subclass of anything, so your code is invalid. Generally, it's best to be as specific as possible when it comes to types, so if you want to create a Ray, then try new Ray
. This will create a new variable of type Ray.
On another note, think about what a Ray is - it requires a start point, a direction and a magnitude. A Vector3 is a 3 dimensional vector, which by definition has a direction and magnitude... however, it doesn't have a start point, and therefore is NOT equivalent to a Ray. In fact, a Ray needs TWO Vector3 objects in order to be fully described, and the Ray class stores these in a way that is useful for Ray calculations.
I suggest, as well as reading this, that you look for some basic Object Oriented Programming tutorials, as well as tutorials for the C# Type system.
Thanks for your answer Hoeloe, but I'd have appreciated more if you described it with a script example. I'm going to edit my question to make it more comprehensible.
I prefer not to hand out working code, because it encourages people to "copy and paste". This is such a simple issue, though. All you need to do is call the constructor for Ray ins$$anonymous$$d of Vector3. When you write new Vector3(x,y,z)
, you're calling a method called a constructor which makes a new Vector3 given the information provided in the arguments. Of course, creating a Ray won't need the same information as a Vector3, so ins$$anonymous$$d you'll get something like this: new Ray(position, direction)
, where position and direction are Vector3s.
@Hoeloe: I'm calling you out on this. Yeah some people may copy/paste, but most will try to understand what the code is doing. For someone who doesn't know what code works, it can be quite difficult to abstractly picture what you're talking about -- especially if their mistake is based in a current misperception of key points.
Aww! Now it's clear to me! I'll have to try that, thanks a lot for explaining yourself! So, i'll edit my question...
Sorry for sounding so harsh, but I have been helping people with code for a long time, and I prefer to explain the concepts behind the code, giving people a push they need to puzzle through it themselves. It takes more work on your part, but at the end you will be guaranteed to understand it. Too many times I have given people exactly what they needed, only finding them asking exactly the same question an hour later because they didn't fully understand it.
Answer by Ashutosh8126 · Sep 29, 2013 at 05:08 PM
Its not the ray i.e Vector3.. Ofcourse it is of type Ray.. is you want to set the origin of the ray... use Ray ray.origin = (Your player's position) .. :)
Hi Ashutosh8126, I tried also that way but kept saying strange things, I'm editing my question to depict a more comprehensible picture of my problem.
Answer by infinitypbr · Sep 29, 2013 at 08:05 PM
Not sure if this will help, but this code works, and will only affect objects marked with the "Player" layer.
var attackForwardMask : LayerMask = -LayerMask.NameToLayer("Player"); // To ensure ray only hits player Layer (only the player is on that layer)
var attackForwardHit : RaycastHit; // Set up for callback
var fwd = transform.TransformDirection (Vector3.forward); // Ray will be shot forward
if (Physics.Raycast (transform.position, fwd, attackForwardHit, radius, attackForwardMask)) // Looking for a hit on a collider within the radius
{
Debug.DrawLine (transform.position, attackForwardHit.point, Color.yellow, 5);
if (attackForwardHit.collider.gameObject.tag == "Player Object") // Double check to make sure it's the player object
{
// DO YOUR STUFF HERE
}
}
Hi sfbaystudios! Thanks for your code! It's really interesting but I'm not sure to fully understand it, but I'll study it with the help of the docs!