Camera.ScreenPointToRay but from GameObject?

Hi, I’m just starting with unity.

I need a function exactly like Camera.ScreenPointToRay but instead of starting from the camera to start from a GameObject I choose. I can’t find a way to do this, thanks in advance.

Ray’s constructor is:
http://unity3d.com/support/documentation/ScriptReference/Ray.Ray.html

I assume you mean you want a ray starting with some object and pointing its direction? If so:

ray = Ray(gameObject.transform.position, gameObject.transform.forward)

But I need it to point to the mouse cursor on screen just like ScreenPointToRay does.

Ah, so pretty close though, no?

So you want a ray from your mouse pointer but in the direction an object is facing? If so, still use the same function just change the parameters.

ray = Ray(camera.ScreenToWorldPoint(), gameObject.transform.forward)

There we’ve changed that. Note, I may have picked the wrong camera function; take a look at:

Specifically, the ToPoint functions.

Hope that helps!

Thats not how it works. ScreenPointToRay receives a point from the screen position (calling it like this Camera.main.ScreenPointToRay(Input.mousePosition)) and returns a ray with origin in the camera and direction the point in screen. This is great for first person shooters because you have the direction of the projectile and you can find out if it collides with anything. Creating a ray using the constructor is useless since I dont want it to always point forward and if I knew the direction I wouldnt have to call ScreenPointToRay in the first place.

The thing is, how do you make third person shooter?, where the projectile still needs to go in the direction of the cursor, but the camera is not in the same position as the player. I havent found a way to create a ray other than with this method, I’ve also tried adding another (non showing) cam to the player but still no luck. Anybody had this problem before? Thanks.

Sorry I wasn’t able to point you in the right direction but my curiosity is getting to me. What do you mean in the direction of the cursor? In my mind, I picture a projectile in a 3rd Person game traveling along a ray out from the player character in the direction its facing.

That, to me, is the same as a first person game, just that the camera is not the same as the character. The cursor on the screen has no impact on which way a project goes.

Are you pointing with the mouse cursor to a position to fire at on the ground?

Sorry if I sound obtuse, I’m really trying to understand your questions, too :slight_smile:

Projectiles dont travel straight forward from the player. It did in the older games like doom, and I think quake1 too. Newer games send the projectiles in the direction of the mouse cursor, so you can shoot someone above or below you, without having to center the enemy onscreen.

I’ve attached some screenshots to show what I’m trying to achieve, thanks.


The problem is that the ray from camera to mouse pointer always goes through all the objects under the mouse pointer, but there exists not a single ray from the player hitting every object under the mouse pointer (since your player is in a different location). The best you can do is construct a ray that hits a single point on the camera to mouse ray. Choosing this point is something you will have to do for the player.

The most obvious solution is to have the player aim for the closest object that your mouse pointer aims at. In other words, perform a raycast from the camera, find the hit point and construct the ray from the player to that point.

Im guessing because you posted SSs of Freelancer you are looking for the same control style i am. This piece of code bellow will get your object to point to the mouse cursor the same as FL does. Its the exact same control style im trying to achieve aswell. Just put it in a JS and attatch it to the object but its only the mouse control now, i havn’t finished the rest yet but it will get you started i think.

var rSpeed = 1.0; // Scale. Speed of the movement 

function FixedUpdate () 
{ 
	
	MousePosition = Input.mousePosition; 
	MousePosition.x = (Screen.height/2) - Input.mousePosition.y;
	MousePosition.y = -(Screen.width/2) + Input.mousePosition.x;
	transform.Rotate(MousePosition * Time.deltaTime * rSpeed, Space.Self);
}

Hi, thanks for the help, I’ll give it a try. But having this in the update wont make the projectile change direction with mouse movement? Shouldnt it be on the Awake method or something like that?

EDIT: Wait, I think that code is for the ship rotation, 'cause it looks like what I did :slight_smile: … now I’m trying to do laser firing stuff.

Ah sorry, i never realised you wanted the projectile to follow the cursor. I was using this before but realised it was no use because it never rotated 360 degress in all directions. But it may work for pointing the guns. I don’t know about the projectile following, though it does use raycast.

    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
    var hitdist = 1000.0;

	var targetPoint = ray.GetPoint(hitdist);
        
	var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
	transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime * smooth);

EDIT I do realise that its still camera point to. But it may point point you in the right direction.

Ah thanks for the screenshots. I was picturing a third person RPG/platform game so was thinking of a different dynamic!

Without looking at the other poster’s code (which may have given you a solution) the idea that comes to my head is still using the camera point to give you your destination for the ray against a special hidden layer that is always in the background… so your ray will collide with that point. Think about a blue screen in a movie. Then you shoot from the player’s ship to that point off in the distance on the fake wall (and beyond).

Don’t know if that would work. Definitely seems a bit tricky. :slight_smile:

Thanks everyone for the tips, I think I’m in the right direction now. This is what I did:

I’ve placed a huge plane-like box collider in front of the player, and then calculate a ray from the cam to this collider (I’ve tried with a sphere collider containing the player first, but this confused the mouse clicking, it selected the back of the sphere and the lasers went backwards :P). Then I point the projectile in the direction of the collision point and set it’s rigidbody speed forward.

It still needs some tweaking, but it’s looking good so far. :smile:

Glad to hear it!

If you get it working perfectly you may want to post it here or even on the wiki. I’m sure others will run into this eventually :slight_smile: