ScreenPointToRay question... (443184)

When using screenPointToRay to get the position of the users mouse (like in the code below), how can you only get the position of only one of the cursor’s axes(so I could only get the x position regardless of the y position)?

var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);

Thanks

You are getting the mousePosition from Input which is a Vector3

var xPos = Input.mousePosition.x;

How would I integrate that into this script(not mine)? It gets the mouse position and then allows an object to look at the mouse, (like this).

function Update () {
	var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var plane : Plane=new Plane(Camera.main.transform.forward, transform.position);
	var dist : float=0.0;
	plane.Raycast(ray, dist);
	transform.LookAt(ray.origin + ray.direction * dist);
}

???

First you ask how to use screenPointToRay without a y value (which by itself cannot be answered easily), then you use an example where the y value has clearly been used.

What are you doing?

What do you mean? I know that script I posted uses the y value. I am wondering how I would change it to not use the y value. I tried to modify it using the bit of code that fireside posted, but I get several errors.

function Update () {
	var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var plane : Plane=new Plane(Camera.main.transform.forward, transform.position);
        var xPos = Input.mousePosition.x;
	var dist : float=0.0;
	plane.Raycast(ray, dist);
	transform.LookAt(xPos * dist);
}

You should not clear the y axis at all, but leave it on the center of the screen. Here’s the code:

function Update () {
        var pos : Vector2 = Input.mousePosition;
        pos.y = Screen.height / 2; //Center of the screen in y axis
	var ray : Ray = Camera.main.ScreenPointToRay (pos);
	var plane : Plane=new Plane(Camera.main.transform.forward, transform.position);
	var dist : float=0.0;
	plane.Raycast(ray, dist);
	transform.LookAt(xPos * dist);
}