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!

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.

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
	}
}

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) … :slight_smile: