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
1
Question by davidflynn2 · Jul 03, 2013 at 09:05 PM · c#rotate

On hover over object

I have a cool idea I am wanting to do but not sure how to do it. I the cube I have in my screen to rotate 90 degress when the mouse hovers over it. How would I start this? I have four different cubes I would like to do this. Each one only do it when I got over it.

Comment
Add comment
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

2 Replies

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

Answer by InfiniBuzz · Jul 03, 2013 at 09:11 PM

Hi

you would create a script and attach it to all of the cubes.

In C# this would look something like this:

using UnityEngine; using System.Collections;

 public class example : MonoBehaviour {
     void OnMouseEnter() {
          transform.Rotate(Vector3.up * 90); // or whatever roatation you want
     }

     // to reset:
     void OnMouseExit() {
          transform.Rotate(*your inverse rotation*);
     }
 }

hope it helps

-edited

over time:

 void OnMouseOver() {
    Vector3 rotation = transform.rotation.eulerAngles;
    if(rotation.y < 90)
       rotation += new Vector3(0, rotation.y + 90 * Time.deltaTime, 0);
    transform.eulerAngles = rotation;
 }

try this, it will rotate your object 90 degrees in one second. If you want it slower, decrease the angles. I didn't test it ;)

Comment
Add comment · Show 7 · 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 clunk47 · Jul 03, 2013 at 09:12 PM 1
Share

Dang got to it before me lol

avatar image ebogguess · Jul 03, 2013 at 09:20 PM 2
Share

Wouldn't On$$anonymous$$ouseOver continue to rotate the screen every frame? Also, depending on @davidflynn2's desired interaction, it might be useful to rotate gradually, rather than jumping 90 degrees in a single frame.

avatar image InfiniBuzz · Jul 03, 2013 at 09:24 PM 1
Share

yes indeed, I edited it to On$$anonymous$$ouseEnter. He didn't say he want it to rotate gradually so I just answered what I thought he want :) but you're right, it would probably look better.

avatar image davidflynn2 · Jul 03, 2013 at 09:56 PM 1
Share

Yes that does what I want what would I have to change to make it happen over time that might improve what im looking at?

I tired adding "* Time.deltaTime" but it don't rotate all the way around then.

avatar image InfiniBuzz · Jul 03, 2013 at 10:09 PM 1
Share

I edited my answer. You need to set it to On$$anonymous$$ouseOver so that it runs every frame. And then you have to check if it already is 90 degrees.

Show more comments
avatar image
2

Answer by ebogguess · Jul 03, 2013 at 09:15 PM

It sounds like you need to get more familiar with performing actions on hover and rotating the camera/screen. OnMouseOver is the method that gets called on every frame that the mouse is over a collider, and OnMouseEnter is a method that is called when the mouse first enters a collider. Camera.main accesses your main camera, and from there you can manipulate it's transform (including rotation).

Comment
Add comment · Show 1 · 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 clunk47 · Jul 03, 2013 at 09:44 PM 1
Share
  • for the correction.

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Object won't rotate correctly 2 Answers

Why is my camera switching angles? 0 Answers

Editor Button Rotates Selected Object 1 Answer


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