Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jacksmash2012 · Jul 29, 2010 at 05:54 PM · mouseperformance

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jacksmash2012 · Jul 29, 2010 at 06:05 PM 0
Share

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?

avatar image qJake · Sep 11, 2010 at 01:42 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by runevision · Jul 30, 2010 at 11:33 AM

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. :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Whimsical · Jul 29, 2010 at 06:31 PM

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

What is SendMouseEvents and why is it allocating memory every frame? 1 Answer

(java script) how to make mouse invisible. 1 Answer

Tons of Scripts, Doing Small things to game. Performance issuses in future? 1 Answer

Mouse cursor on Windows 8 0 Answers

Pro-water Shader causing large jump in draw calls 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges