# Get touch position on perspective camera.

I am working on my very first IOS game, I don’t have an apple developer license yet so i am testing the game using mouse click. In this prototype stage want to be able essentially “draw” red cylinders (lava) on a plane a slight angle from the camera. I will be able to change this code to Mobil format fairly easily, but there is a bit of a problem

the code only works when the game is in orthographic mode.

``````var particlething : GameObject;
var lasthit : RaycastHit;
var hit : RaycastHit;
var cylinderPrefab : GameObject;

function CreateCylinderBetweenPoints(start : Vector3, end : Vector3, width : float)
{
var offset = end - start;
var scale = new Vector3(width, offset.magnitude / 2.0, width);
var position = start + (offset / 2.0);

var cylinder = Instantiate(cylinderPrefab, position, Quaternion.identity);
cylinder.transform.up = offset;
cylinder.transform.localScale = scale;
}
function Update (){

var p : Vector3 = camera.ScreenToWorldPoint (Vector3(Input.mousePosition.x,Input.mousePosition.y,0.1));

if(Input.GetMouseButton(0) == true){

var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (p, fwd, hit)) {

if(lasthit != null){

CreateCylinderBetweenPoints(hit.point, lasthit.point, 1);

}

lasthit = hit;
}
}
}
``````

the perspective camera throws the script off. Although an orthographic camera is okay, I would prefer a perspective one (Only If it isn’t going to be to hard to make it into perspective. because this is my first game, i can live without some visual effects.)

is there any easy way to do what i am trying to do?

When the camera is an an angle, one solution is to use Unity’s mathematical Plane class and do a Plane.Raycast() against that plane. A plane is defined by a point in space and a normal to that plane. In the test code below I’ve use Vector3.back as the normal, but I’m am unsure of your goal here. You may want cam.transform.forward, or Vector3.up depending on the “surface” you want to Raycast against. Here is some example code:

``````#pragma strict

public var cylinderPrefab : GameObject;
public var cam : Camera;

private var lastPoint : Vector3;
private var plane : Plane;

function Start() {
if (cam == null) {
cam = Camera.main;
}
plane = new Plane(Vector3.back, Vector3.zero);
}

function CreateCylinderBetweenPoints(start : Vector3, end : Vector3, width : float) {
var offset = end - start;
var scale = new Vector3(width, offset.magnitude / 2.0, width);
var position = start + (offset / 2.0);

var cylinder = Instantiate(cylinderPrefab, position, Quaternion.identity);
cylinder.transform.up = offset;
cylinder.transform.localScale = scale;
}

function Update () {

if(Input.GetMouseButton(0)){

var ray = cam.ScreenPointToRay(Input.mousePosition);
var enter : float;
if (plane.Raycast(ray, enter)) {
var point = ray.GetPoint(enter);

if(!Input.GetMouseButtonDown(0) && lastPoint != point){
CreateCylinderBetweenPoints(lastPoint, point, 1);
}
lastPoint = point;
}
}
}
``````