x


Is Update() the best way to handle mouse motion?

I'm making a simple 2D game, where a ship in the middle of the screen points to the mouse location. This is working no problem, as I've put some code in the Update() function to track the mouse location and point the ship to look at it:

var mousePos = Input.mousePosition;
mousePos.z = -(transform.position.y - Camera.mainCamera.transform.position.y);
var worldPos = Camera.mainCamera.ScreenToWorldPoint(mousePos);
transform.LookAt(worldPos);

However, I'm finding a noticeable performance hit when I use this approach, i.e., there is a bit of lag every time I move the mouse. I was thinking that maybe there's an Input.OnMouseMotion event handler, but no such luck.

Just wondering if there's a better way?

more ▼

asked Jul 29, 2010 at 05:54 PM

jacksmash2012 gravatar image

jacksmash2012
557 120 94 107

I just built my project and ran it is a web-browser, and I certainly do not notice the same performance hit. Perhaps it's just a result of running it in the Unity environment itself?

Jul 29, 2010 at 06:05 PM jacksmash2012

In short - Yes, it's the best way, and it's not a "Unity thing", either - all games handle input through the "Update()" function one way or another.

Sep 11, 2010 at 01:42 AM qJake
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

The performance hit is probably not related to the mouse at all; none of the lines would be faster or slower depending on if the mouse moves or not.

However, since the transform will only be changed if the mouse moves, maybe that could be something to look into. For example, if you have a collider on the object you've moving, but no rigidbody, then there's a performance penalty in moving the object since it's assumed to be static (CharacterControllers are the exception from this).

Also, if you don't see any performance hit when building a player, then it's probably not a problem in your code. :)

more ▼

answered Jul 30, 2010 at 11:33 AM

runevision gravatar image

runevision ♦♦
9.2k 54 110 173

(comments are locked)
10|3000 characters needed characters left

Without knowing what exactly else is happening in your project it's hard to tell what actually causes the performance bump. But it's save to say that Update() is the best place to put your functionality, given that you want it to be evaluated once every single frame. And there's no function in your four lines of code that is unnecessarily performance hungry, so by judging on these four lines of code: There isn't supposed to be a lack whatsoever.

What else are you doing when you move your ship?

more ▼

answered Jul 29, 2010 at 06:31 PM

Whimsical gravatar image

Whimsical
285 15 16 32

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1445
x1113

asked: Jul 29, 2010 at 05:54 PM

Seen: 1416 times

Last Updated: Jul 29, 2010 at 08:01 PM