Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 SnowfireGaming · Jan 26, 2014 at 07:17 AM · rotationjavascriptgameobjectlocalrotation

Link object rotation to its parent

I am reasonably new to programming in unity and I have been stuck with this issue for a couple of days now...

What I want is for an object that is instantiated on the press of a button then set to the child of the FPS Controller to always be facing the camera when you look around.

My current code is this (Its probably very messy):

 var objectOut = false;
 
 var prefabs : GameObject[];
 
 var maxDistance = 5;
 var minDistance = 1;
 
 var obj;
 
 function Update () {
      Screen.lockCursor = true;
     if(Input.GetKeyUp("1") || Input.GetKeyUp("2") || Input.GetKeyUp("3") || Input.GetKeyUp("4")){
         if(objectOut == false){
             objectOut = true;
             
             if(Input.GetKeyUp("1")){
                 obj = Instantiate (prefabs[0], Vector3(0, 0, 0), Quaternion.identity);
             }else if(Input.GetKeyUp("2")){
                 obj = Instantiate (prefabs[1], Vector3(0, 0, 0), Quaternion.identity);
             }else if(Input.GetKeyUp("3")){
                 obj = Instantiate (prefabs[2], Vector3(0, 0, 0), Quaternion.identity);
             }else if(Input.GetKeyUp("4")){
                 obj = Instantiate (prefabs[3], Vector3(0, 0, 0), Quaternion.identity);
             }
             
             obj.transform.collider.enabled = true;
             obj.layer = 2;
             obj.transform.parent = gameObject.transform;
             obj.transform.rotation = gameObject.transform.rotation;
             obj.renderer.material.color.a = 0.5;
             
             var Hit : RaycastHit;
             Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), Hit);
             
             obj.transform.localPosition = Vector3(0,2,1);
         }else{
             Destroy(obj);
             objectOut = false;
         }
     }
     
     if(objectOut){
         if(Input.GetAxis("Fire1")){
             objectOut = false;
             obj.transform.parent = null;
             obj.layer = 0;
             obj.transform.collider.enabled = true;
         }else{
             
             var rcHit : RaycastHit;
             var theRay : Vector3 = obj.transform.TransformDirection(Vector3.down);
             
             if (Physics.Raycast(obj.transform.position, theRay, rcHit)){
                 var GroundDis = rcHit.distance;
                 obj.transform.rotation = Quaternion.FromToRotation(Vector3.up, rcHit.normal);
                 obj.transform.localPosition.y = (obj.transform.localPosition.y - GroundDis) + obj.collider.bounds.size.y / 2;
             }
             
             if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), Hit, maxDistance)){
                 if(Hit.distance >= minDistance){
                     obj.transform.position.x = Hit.point.x;
                     obj.transform.position.z = Hit.point.z;
                 }else{
                     obj.transform.localPosition.z = minDistance;
                 }
             }else{
                 obj.transform.localPosition.z = maxDistance;
             }
         }    
     }
 }

When I add this:

 obj.transform.localRotation.w = 0;

which I would expect to work, it results in some weird rotation of the object and even the object sinking through the ground when I move it around on slopes...

Any help would be appreciated!

Comment
Add comment · Show 12
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 getyour411 · Jan 26, 2014 at 08:47 AM 0
Share

You do not want to be messing around with the localRotation Quaternion unless you really know Quaternions.

avatar image Invertex · Jan 26, 2014 at 09:04 AM 0
Share

Yeah, don't set rotation directly. Unity doesn't use a classic rotation method, it uses Quaternions, which avoid the issue of gimbal lock.

You could try: https://docs.unity3d.com/Documentation/ScriptReference/Quaternion.LookRotation.html

avatar image SnowfireGaming · Jan 26, 2014 at 09:05 AM 0
Share

I actually haven't tried anything else because I don't know what to try... And thanks for the tip :D

avatar image SnowfireGaming · Jan 26, 2014 at 09:16 AM 0
Share

Invertex, that works in the sense that it keeps my object facing me (or away from me) but it loses the rotation to fit the ground, how would I make it do both?

avatar image Invertex · Jan 26, 2014 at 09:21 AM 0
Share

For the upwards direction, use transform.up ins$$anonymous$$d. Should work I believe...

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Demigiant · Jan 26, 2014 at 11:31 AM

Inside LateUpdate (so you're sure it's executed before camera render but after the parent transform has changed - if he uses Update), just write:

 myFacingObject.transform.LookAt(myCamera.transform, Vector3.up);

Also, since you would be using your object's and your camera's transforms at every update, I'd suggest caching them in a couple of variables.

Comment
Add comment · Show 2 · 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 SnowfireGaming · Jan 26, 2014 at 12:06 PM 0
Share

This works but it ends up with results like this: http://i.imgur.com/aO0k$$anonymous$$qj.png where I want it to link to the terrain like this: http://i.imgur.com/L3opTBS.png

avatar image Demigiant · Jan 26, 2014 at 05:41 PM 0
Share

Sorry, didn't understand that that was what you meant. In that case, you'd have to calculate a vector perpendicular to the terrain, so you can pass it ins$$anonymous$$d of Vector3.up

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

20 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 avatar image

Related Questions

Rotating an object in position 2 Answers

Point game object to another only on the Local X Axis 0 Answers

using a custom rotation in "Instantiate" 4 Answers

How do you flip a card with smooth rotation? 1 Answer

Character Rotation Now Matching GameObject's Local Rotation 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