# ScreenPointToRay question... (443184)

**URL:** https://discussions.unity.com/t/screenpointtoray-question-443184/443184
**Category:** Scripting
**Tags:** Scripting
**Created:** [April 23, 2011, 9:17pm UTC](https://discussions.unity.com/t/screenpointtoray-question-443184/443184 "2011-04-23T21:17:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![anon20000101](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@anon20000101](https://discussions.unity.com/u/anon20000101)
#### Post date: [April 23, 2011, 9:17pm UTC](https://discussions.unity.com/t/screenpointtoray-question-443184/443184/1 "2011-04-23T21:17:36Z")

</div>

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)?

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

```

Thanks

---

<div class="post-metadata">

### Author: ![anon\_82115715](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@anon\_82115715](https://discussions.unity.com/u/anon_82115715)
#### Post date: [April 24, 2011, 4:17am UTC](https://discussions.unity.com/t/screenpointtoray-question-443184/443184/2 "2011-04-24T04:17:33Z")

</div>

You are getting the mousePosition from Input which is a Vector3

var xPos = Input.mousePosition.x;

---

<div class="post-metadata">

### Author: ![anon20000101](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@anon20000101](https://discussions.unity.com/u/anon20000101)
#### Post date: [April 24, 2011, 4:34am UTC](https://discussions.unity.com/t/screenpointtoray-question-443184/443184/3 "2011-04-24T04:34:18Z")

</div>

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](http://www.matteley.co.uk/labs/unity3d-spacerunner/)).

```auto
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);
}

```

---

<div class="post-metadata">

### Author: ![npsf3000\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/npsf3000_1/32/466497_2.png) [@npsf3000\_1](https://discussions.unity.com/u/npsf3000_1)
#### Post date: [April 24, 2011, 5:33am UTC](https://discussions.unity.com/t/screenpointtoray-question-443184/443184/4 "2011-04-24T05:33:21Z")

</div>

> [@system](#):
>
> 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](http://www.matteley.co.uk/labs/unity3d-spacerunner/)).

???

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?

---

<div class="post-metadata">

### Author: ![anon20000101](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@anon20000101](https://discussions.unity.com/u/anon20000101)
#### Post date: [April 24, 2011, 5:43am UTC](https://discussions.unity.com/t/screenpointtoray-question-443184/443184/5 "2011-04-24T05:43:19Z")

</div>

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.

```auto
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);
}

```

---

<div class="post-metadata">

### Author: ![sangi93](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@sangi93](https://discussions.unity.com/u/sangi93)
#### Post date: [April 24, 2011, 5:52am UTC](https://discussions.unity.com/t/screenpointtoray-question-443184/443184/6 "2011-04-24T05:52:12Z")

</div>

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

```auto
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);
}

```
