Object follow mouse in open space - (no terrain for RaycastHit)

Hi folks!

I’ve been busting my brains the whole morning trying to figure this one out, searching for an answer and now, since time is passing and I did nothing, decided to ask here.

I’m working on a first person space shooter. A simulation of piloting in space with real space physics.

The issue is that I would very much like to have my spaceship turrets fire in a direction I’m pointing with a mouse, but mouse is free, not fixed to a center of the screen. So I guess it would be best if I added an empty game object to follow the mouse at a certain distance and aim turrets towards it. But since there is nothing for a raycast to hit, and I would like to avoid a giant transparent plane in front of a camera, I have no idea whatsoever how to do this.

Any help would be appreciated.

Thx!

Is the following what you need?

Camera.ScreenToWorldPoint
or
Camera.ViewportToWorldPoint

@mhaddon: I guess Camera.ViewportToWorldPoint could lead to translating Vector2 mouse position to a Vector3 an than pushing that position to a certain distance from the camera/player and then translating the empty game object to that position.

I’ll have to study this a bit more :slight_smile:

Thanks!

@mhaddon: It’s alive!!!

Here’s the code:

var mousePosition;
var camera1 : Camera;
var AimingPointDistance : float;
var AimingPointPosition : Vector3;


function FixedUpdate(){

mousePosition = Input.mousePosition;
AimingPointDistance = camera1.transform.position.z + 20000;

AimingPointPosition = camera1.ScreenToWorldPoint(Vector3(mousePosition.x,mousePosition.y,AimingPointDistance));

transform.position = AimingPointPosition;

}

Big thanx again!

:wink:

celinscak,

How do you implement this? I’m also looking to control turret with the mouse, infact I wish to control a batch of turrets. Will this work?