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 NickP_2 · Jul 14, 2013 at 02:52 AM · camerarotationrotatefloating

Float the camera back to his position

I have camera around my player, when I hold down right click it'll rotate around the player. But when I release the right click, I want it to go back to its original position/rotation. To rotate:

  if(Input.GetMouseButton(1) == true)
         {
             rotateSpeed =  Input.GetAxis("Mouse X") * speed;
         transform.RotateAround(player.transform.position, Vector3.up, rotateSpeed);
         }


When the camera isn't rotating anymore, a timer will count down from 3 seconds, after this the camera should float nicely back to the standard position, I thought to reverse the actions but this doesn't work:

 public void ReturnCamera()
     {
         if(timeLeft <= 0)
         {
                     //changed Vector3.up to vector3.down
             transform.RotateAround(player.transform.position, Vector3.down, rotateSpeed);
             timeLeft = 3;
         }
     }
Comment
Add comment · Show 4
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 Benproductions1 · Jul 14, 2013 at 02:59 AM 1
Share

Just as a note, you never have to (and never should) write == true or == false. An if statement, evaluates the information in the brackets to either true or false.
You're basically going either:
true == true
or
false == true

Therefore there is no need for == true.

There is also never a need for == false as you can just invert the value using ! (not).

Heres some simple boolean expressions to get you on track :)

 if (Input.GetButtonDown("Fire") == true)
 //same as
 if (Input.GetButtonDown("Fire"))
 
 if (Input.Get$$anonymous$$ouseButton(1) == false)
 //same as
 if (!Input.Get$$anonymous$$ouseButton(1))
 
 if (timeLeft <= 0)
 //same as
 if ((timeLeft <= 0) == true)
 
 if (!(timeLeft <= 0))
 //same as
 if (timeLeft > 0)

The less code (more shorthands), the easier to read, the better the code :)

avatar image robertbu · Jul 14, 2013 at 04:55 AM 1
Share

The first issue is that your Get$$anonymous$$ouseButton(1) code gets executed for all the frames that the button is held down, but your ReturnCamera() code only gets executed for a single frame. That is, on line 7 you do: timeLeft = 3;. This stops any rotation, so the first time your ReturnCamera() is called, it stops any work done on subsequent calls.

The larger issue is that you don't have any code here that represents 'original position/rotation.' In fact, it is possible that when the mouse button is lifted the rotation is back to the original or past the original one or more times.

In order for someone to offer an accurate solution, they will need to know more about the problem:

  • Does the object have to rotate back to the original position on the same axes?

  • Does the object have to rotate back to the original position in the reverse direction of how it got to its position?

  • Does the object have to reverse the path exactly. For example if the object is rotated two times around, should it do a reverse rotation of two times?

avatar image NickP_2 · Jul 14, 2013 at 02:28 PM 0
Share

Wow, thanks guys, I'm learning allot here ! And About the floating back; The standard position is right behind the player. It doesn't matter where he rotated to, it should always go back BEHIND the player :)

So if the players position is (0, 0, 0), the standard position of the camera should be (0, 0, -1) and looking in the direction of the player. Thanks !

avatar image NickP_2 · Jul 14, 2013 at 02:31 PM 0
Share

@robertbu, That's makes so much sence... $$anonymous$$aby I could figure it out now

1 Reply

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

Answer by robertbu · Jul 14, 2013 at 04:13 PM

  • Place an empty game object at the same position and rotation of your character.

  • Make that empty game object a child of the character

  • Place your camera so that is is look at the back of your character just the want you want it.

  • Make the camera a child of the empty game object.

Attach this script to the empty game object:

 #pragma strict
 
 var speed = 5.0;
 var returnSpeed = 90.0;
 
 function LateUpdate() {
 Debug.Log(Input.GetAxis("Mouse X"));
     if(Input.GetMouseButton(1) == true) {
             var rotateSpeed =  Input.GetAxis("Mouse X") * speed;
             var q = Quaternion.AngleAxis(rotateSpeed, Vector3.up);
             transform.localRotation = q * transform.localRotation;
         }
     else
         transform.localRotation = Quaternion.RotateTowards(transform.localRotation, Quaternion.identity, returnSpeed * Time.deltaTime);
 }

It uses local rotation, so the empty/game object will always follow the rotation of the character. A local rotation of (0,0,0), which is Quaternion.identity, will place the camera back behind the character.

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 NickP_2 · Jul 14, 2013 at 06:23 PM 0
Share

Ha, works like a charm !

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

16 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

Related Questions

Object rotates with camera 2 Answers

Return Camera Rotation Z axis to 0 2 Answers

Wonky Camera Behaviours 0 Answers

I am getting a problem with moving and rotating a camera! 1 Answer

Help with camera snap 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