Screen to World

Hello, i`m starting a project here were i need to make a Arcade style vertical scrolling game, where the camera follows my character from an angle at his back. How do i get a world position using my mouse?

I mean, theres a crosshair already drawn in my GUI and id like to shoot at the direction of that crosshair, the shoot will go from my character to my aiming point in the 3d world. Ignoring the Y axis, cause my character moves forward in the Z axis and sideways in the X axis.

Thanks in advance.

If the camera is always the same distance from the 2D level, eg 10 units, you could just multiply that with the direction vector of your camera and transform the local vector to world vector! The resulting vector is the position where you want your bullet to go.

Search for anything you need in the script reference! eg ‘camera’ or ‘local’…

Hmmm the main problem is that my camera is not ortographic neither my character is always in it`s center, I uploaded an example of what is my field of view here: http://kauelima.com/unity/jeep/

Theres already a test script there but as you can see my turret dont move as it should =/

For test purpouses you can use the C key to show were the turret is facing.

Hey there,

I am doing something similar and I did it this way:

Use Camera.ScreenPointToRay(Input.mousePosition)
This will return you a ray, use that to cast a ray with Physics.Raycast() and the resulting RaycastHit will give you the world position.

Something like this in C#(this code is not tested)

            Ray myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(myRay, out hitInfo))
            {
                Vector3 worldPosition = hitInfo.point;
            }
public var player : Transform;
public var target : Transform;

private var distancePlayerCamera : float;

function Awake ()
{
	distancePlayerCamera = transform.InverseTransformPoint (player.position).z;
}

function Update ()
{
	var mousePos : Vector3 = Input.mousePosition;
	var targetPosition : Vector3 = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, distancePlayerCamera));
	
	target.position = targetPosition;
}

Try this (with player being your ship, and target, an empty game object).

@AkilaeTribe the targetPosition var is returning a null value for some weird reason =/

@osmant hmmm well your solution didn’t work for the first time but i think i’ll try something near to what you said that came to my head =O

Hey, I just tried this script, and it seems to work for me:

using UnityEngine;
using System.Collections;

public class RayCastTest : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
    Ray r;
    RaycastHit HitInfo;
	// Update is called once per frame
	void Update () {
        r = Camera.main.ScreenPointToRay(Input.mousePosition);
        if(Physics.Raycast(r,out HitInfo))
        {
            Vector3 worldPos = HitInfo.point;
            transform.position = worldPos;
        }
	
	}
}

Attach that script to any object, and it will position it at your mouse position.(turn off any colliders on the object that will use this script, otherwise it will raycast on itself)

This is not tested with terrain, but anything with a collider, it works here.