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
2
Question by Clintmcc · May 28, 2015 at 06:31 AM · camerarotationrotatepickup

Object rotates with camera

Hi there,

I have written a script to pick up an object using unity's FPcontroller prefab. So far I have the object being picked up and dropped when using left click. if your holding the object and hold down the right mouse button the object moves to the center of the screen and rotates along the objects Y axis. This is all working fine except if I look up or down while holding the object, it sticks to its world rotate along the Z axis. So if I look up and rotate the object I am looking at the bottom of it. I want the objects Z axis to follow the camera so no matter what direction you're looking you can always see it from the front, not the bottom or top. Here is the code:

     void PickupObject(){
             transform.rotation = Quaternion.identity;
             rigidbody.isKinematic = true;
             Collider collider = gameObject.GetComponent<Collider> ();
             collider.isTrigger = true;
             rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint(DistanceFromCamera);
             this.transform.parent = Camera.main.transform;
             InteractObject script = GameObject.Find (thisObject).GetComponent<InteractObject>();
             script.enabled = false;
             PlayerRay rayScript = GameObject.Find ("FirstPersonCharacter").GetComponent<PlayerRay>();
             rayScript.EnableRay (false);
         }
     
         void DropObject(){
             isHitting = false;
             //Debug.Log ("dropping");
             rigidbody.isKinematic = false;
             Collider collider = gameObject.GetComponent<Collider> ();
             collider.isTrigger = false;
             rigidbody.WakeUp ();
             rigidbody.AddForce (go.transform.forward * throwForce, ForceMode.Impulse);
             this.transform.parent = null;
             InteractObject script = GameObject.Find (thisObject).GetComponent<InteractObject>();
             script.enabled = true;
             PlayerRay rayScript = GameObject.Find ("FirstPersonCharacter").GetComponent<PlayerRay>();
             rayScript.EnableRay (true);
             bool fpsScript;
         }
     
         void RotateObject(){
             if (Input.GetButton ("Fire2")) {
                 DistanceFromCamera = new Vector3 (0.5f, 0.5f, DistanceHeldFromCamera);
                 rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
                 bool fpsScript;
                 fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = false;
                 transform.eulerAngles=new Vector3(0,transform.eulerAngles.y,0);
                 transform.Rotate (0, (Input.GetAxis ("Mouse X") * RotationSpeed * Time.deltaTime), 0, Space.World);
             } else if (Input.GetButtonUp ("Fire2")) {
                 DistanceFromCamera = new Vector3(0.5f,-0.0f,DistanceHeldFromCamera);
                 rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
                 fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true;
             }
         }

Any help on this is much appreciated. Thank you :)

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
0
Best Answer

Answer by Clintmcc · May 30, 2015 at 03:52 PM

I always feel silly but satisfied when I find the answer but its a mistake I had made.

I just needed to change the rotate line from Space.World to Space.Self.

Thanks Kuba for your contribution. Lookat was the answer to the first problem and Space.self the second problem. Thank you.

Here's the final code if anyone else gets stuck.

 void RotateObject(){
 
         if (Input.GetButton ("Fire2")) {
             DistanceFromCamera = new Vector3 (0.5f, 0.5f, DistanceHeldFromCamera);
             rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
             bool fpsScript;
             fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = false;
             transform.Rotate (0, (Input.GetAxis ("Mouse X") * RotationSpeed * Time.deltaTime), 0, Space.Self);
             if(!looking){
                 transform.eulerAngles=new Vector3(0,transform.eulerAngles.y,0);    
                 Transform target = Camera.main.transform;
                 transform.LookAt(target, Vector3.up);
                 looking = true;
             }
         } else if (Input.GetButtonUp ("Fire2")) {
             DistanceFromCamera = new Vector3(0.5f,-0.0f,DistanceHeldFromCamera);
             rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
             bool fpsScript;
             fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true;
             looking = false;
 
         }
     }
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 KubaPrusak · May 28, 2015 at 01:09 PM

If you want the object to face the player you can use

 transform.LookAt(Transform target, Vector3 worldUp)

where target would be the player Camera. worldUp is an optional parameter but you should set it to transform.forward; if the object doesn't rotate with the correct side facing the player, you can try using different Vectors (ie. transform.up, transform.right, transform.back etc).

transform.LookAt and all of the transform.forward/up's refer to the transform of the object you are holding, not the player.

Comment
Add comment · Show 13 · 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 Clintmcc · May 28, 2015 at 02:22 PM 0
Share

Hi, thanks for your response.

This solution solves the problem but creates a new one. I lose the ability to rotate the object because the Lookat is locking it in place looking towards the camera.

 void RotateObject(){
     if (Input.GetButton ("Fire2")) {
         DistanceFromCamera = new Vector3 (0.5f, 0.5f, DistanceHeldFromCamera);
         rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
         bool fpsScript;
         fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = false;
         transform.eulerAngles=new Vector3(0,transform.eulerAngles.y,0);
         transform.Rotate (0, (Input.GetAxis ("$$anonymous$$ouse X") * RotationSpeed * Time.deltaTime), 0, Space.World);
         Transform target = Camera.main.transform;
         transform.LookAt(target, Vector3.up);
     } else if (Input.GetButtonUp ("Fire2")) {
         DistanceFromCamera = new Vector3(0.5f,-0.0f,DistanceHeldFromCamera);
         rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
         bool fpsScript;
         fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true;

     }
 }
avatar image KubaPrusak · May 28, 2015 at 02:56 PM 1
Share

Why dont you create a parent to the object you pick up ie.

Object

-Actual model

The Object is a parent of the actual model (ie mesh etc.)

The actual model is the mesh etc and the object is the thing you pick up in your script. Then keep the script as is but make the transform.LookAt reference the transform of the actual model.

So the object rotates with your script and the mesh faces the player.

There is probably a better way to do this but thats all I've got.

avatar image Clintmcc · May 28, 2015 at 03:00 PM 0
Share

I got this solution working by ensuring it doesn't run every frame with a condition. What is happening now is that if your looking forward, you can rotate the object around it's Y Axis with no problem. Looking up or down and doing it though, the Y Axis changes on the object because its being rotated. I'm not sure if I'm explaining it very well so i'll include the code and a couple of images.

 void RotateObject(){

     if (Input.GetButton ("Fire2")) {
         DistanceFromCamera = new Vector3 (0.5f, 0.5f, DistanceHeldFromCamera);
         rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
         bool fpsScript;
         fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = false;
         transform.Rotate (0, (Input.GetAxis ("$$anonymous$$ouse X") * RotationSpeed * Time.deltaTime), 0, Space.World);
         if(!looking){
             transform.eulerAngles=new Vector3(0,transform.eulerAngles.y,0);    
             Transform target = Camera.main.transform;
             transform.LookAt(target, Vector3.up);
             looking = true;
         }
         //transform.rotation = Quaternion.LookRotation(target, Vector3.up);
     } else if (Input.GetButtonUp ("Fire2")) {
         DistanceFromCamera = new Vector3(0.5f,-0.0f,DistanceHeldFromCamera);
         rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
         bool fpsScript;
         fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true;
         looking = false;

     }
 }

Looking Forward

Looking Up

lookingforward.jpg (70.1 kB)
looking-up.jpg (58.5 kB)
avatar image Clintmcc · May 28, 2015 at 03:09 PM 0
Share

The parenting to an empty GO might work. I'll give this a try tomorrow and update the post. Thanks for the effort :)

avatar image Clintmcc · May 28, 2015 at 03:36 PM 0
Share

Parenting is also not working :( Same result.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Viewmodel/camera rotation when moving 1 Answer

Movement with camera and rotation of objects 1 Answer

How to rotate my camera? 2 Answers

Float the camera back to his position 1 Answer

My character suddenly stopped looking up/down 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