• 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 dot · Dec 25, 2010 at 01:58 PM · rotate

custom rotate object - according to camera viewports_problem

I am looking for something custom rotate function that would work like this:

  • you press space, the script saves the viewPortPoint where the mouse cursor have been at the time of the space press. it's a "mouseClick" variable in the example below.

  • all the time while the space is pressed, you can move the mouse cursor around and the game calculates what distance is it from "mouseClick" to the current Input.mousePosition, and direction (in 2d)

-then the object rotates accordingly - transform.Rotate(...); with growing distance from the "mouseClick" the speed of the rotation is faster, it's a "distance modifier - and the direction of rotation is as follows - if Y of the mouseClick and currentMousePosition is the same, and only X is lower - then the direction of rotation is Vector3(-1,0,0) (// distanceModifier Time.deltaTime) ; if X is higher, then rotation is Vector3(1,0,0),if Y is different and X stays the same you get (0,-1,0) // distanceModifier time(...), etc. you get the idea. also, ofcourse if X is higher and Y is higher then you get it proportionally for example: direction = Vector3(0.2, 0.6,0) or Vecto3(-.9, .7,0) or Vector3(-.6,-.8) and anything in between. you get the idea.

the problem is that when I use this script, as an attempt to do it

var direction = Vector3.right; var speed = 1; function Update () { rotation(); }

var rotate = false; var mouseClick = Vector3.zero; var scroolSpeed = Vector3.zero; function rotation(){ //just when pressed the button - get the coordinates of the button press, so that we'll count the angle and distance from that point if(Input.GetKeyDown(KeyCode.Space)){ rotate = true; //turn on mouseClick = Camera.main.ScreenToViewportPoint(Input.mousePosition); // get click in viewport }

if(rotate){ //DISTANCE var mouseInput = Camera.main.ScreenToViewportPoint(Input.mousePosition); //every turn's mouse input var mouseDistance = mouseInput - mouseClick; //distance in viewports

scroolSpeed = mouseDistance; // orbiting strnght - proportional to distance from clicked - spaced point //DIRECTION

//ROTATION transform.Rotate(scroolSpeed speed Time.deltaTime); //rotation calculated using the direction and the proportional power. }

if(Input.GetKeyUp(KeyCode.Space)) rotate = false; //turn off }

and I press space - Unity crashes. can you give me a hand with the mistakes I'm making here? Thanks.

Comment
Add comment · Show 1
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 dot · Dec 25, 2010 at 02:22 PM 0
Share

I guess, never$$anonymous$$d. when I had "while(rotate)" condition, it crashed. then I moved and renamed scripts and it seems I was still saving changes to the old one... eh :). Still, if someone can use or improve the script, it's out there for you.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Camera rotation around player while following. 6 Answers

How do I rotate object smoothly 3 Answers

How to have vertical and horizontal axis work at the same time 1 Answer

Why does transform.Rotate( x, y, z ) work correctly but transform.Rotate( Vector3.forward ) does not? 1 Answer

Sidescroller Turning 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