Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by dodget90 · May 12, 2017 at 07:31 PM · scripting problemscripting beginnerrotation axisparent and childpick up

Parent Axis and Child Axis issue

hi guys I have an issue and I feel like i have exhausted every avenue. I have an object pickup script attached to a box and when I pick up the box it parents to my playerCam and then I can use the scroll wheel to rotate the box forwards and backwards which is working fine as I want it to.

shown here the white arrow is player direction and the red arrow is the box rotation. alt text

the issue I'm having is if I grab it from the side it uses its own axis so when I try to rotate the box forwards or backwards again it rotates left and right as shown in the image below. I would like it to always rotate forwards from the player no matter how I pick it up. I don't want the box to change its position on pickup. For example, if I pick it up from the corner, that corner will still be facing me but I can still rotate it towards or away from the player.

shown here the white arrow is player direction and the red arrow is the box rotation and the green arrow is how I would like the box to rotate. alt text

I hope what i have said is understandable to you. Below is the script I have attached to my box. I've struggled with this for a couple of weeks now and had every outcome apart from what's needed. Any help at all would be greatly appreciated.

Thanks in advance.

  using UnityEngine;
  using System.Collections;
 
     public class PickUp : MonoBehaviour 
 {
     
     public Transform player;
     public Transform playerCam;
     public Transform item;
     public float throwForce = 400;
     public bool hasPlayer = false;
     public bool beingCarried = false;
     public float turnSpeed = 400f;
     float rotSpeed = 20;
 
     void Start ()
      {
         }
     void Update ()
     {
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit hit;
 
         if (Physics.Raycast (ray, out hit, 1)) {
             hasPlayer = true;
         } else {
             hasPlayer = false;
         }
         if (hasPlayer && Input.GetMouseButton(0)) {
             GetComponent<Rigidbody>().isKinematic = true;
             item.transform.parent = playerCam.transform;
             beingCarried = true;
         }
         if (beingCarried) {
             RotateFunction ();
             }
             if (Input.GetKeyDown (KeyCode.Mouse0)) {
                 GetComponent<Rigidbody> ().isKinematic = false;
                 transform.parent = null;
                 beingCarried = false;
                 GetComponent<Rigidbody> ().AddForce (playerCam.forward * throwForce);
             } else if (Input.GetKeyDown (KeyCode.Mouse1)) {
                 GetComponent<Rigidbody> ().isKinematic = false;
                 transform.parent = null;
                 beingCarried = false;
             }
         }
     void RotateFunction()
     {
         if (Input.GetAxis("Mouse ScrollWheel") > 0f ) // forwards
             {
             item.transform.Rotate(Vector3.right, turnSpeed * Time.deltaTime);
         }         
             else if (Input.GetAxis("Mouse ScrollWheel") < 0f ) // backwards
                 {
             item.transform.Rotate(Vector3.right, -turnSpeed * Time.deltaTime);
             }
         }
     }



pickedupfront.jpg (195.6 kB)
pickedupside.jpg (212.0 kB)
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

1 Reply

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

Answer by dodget90 · May 16, 2017 at 07:48 PM

I solved the question and here are the changes if anyone else needs to know. I had to use an empty game object as a child of the box and then un child it when i pick up the box, rotate it to match the player cam and then parent the box to the empty game object as not to change the rotation of the box. then when I let go of the box I reparent the empty game object to the box again and it works just as I wanted.

   using UnityEngine;
   using System.Collections;
  
      public class PickUp : MonoBehaviour 
  {
      
      public Transform player;
      public Transform playerCam;
      public Transform item;
      public Transform empty;
      public float throwForce = 400;
      public bool hasPlayer = false;
      public bool beingCarried = false;
      public float turnSpeed = 400f;
      float rotSpeed = 20;
  
      void Start ()
       {
          }
      void Update ()
      {
          Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
          RaycastHit hit;
  
          if (Physics.Raycast (ray, out hit, 1)) {
              hasPlayer = true;
          } else {
              hasPlayer = false;
          }
          if (hasPlayer && Input.GetMouseButton(0)) {
              GetComponent<Rigidbody>().isKinematic = true;
                      empty.transform.parent = null;
             empty.transform.rotation = playerCam.transform.rotation;
             item.transform.parent = empty.transform;
             empty.transform.parent = playerCam.transform;
              beingCarried = true;
          }
          if (beingCarried) {
              RotateFunction ();
              }
              if (Input.GetKeyDown (KeyCode.Mouse0)) {
                  GetComponent<Rigidbody> ().isKinematic = false;
                  transform.parent = null;
                  empty.transform.parent = item.transform;
                  beingCarried = false;
                  GetComponent<Rigidbody> ().AddForce (playerCam.forward * throwForce);
              } else if (Input.GetKeyDown (KeyCode.Mouse1)) {
                  GetComponent<Rigidbody> ().isKinematic = false;
                  transform.parent = null;
                  empty.transform.parent = item.transform;
                  beingCarried = false;
              }
          }
      void RotateFunction()
      {
          if (Input.GetAxis("Mouse ScrollWheel") > 0f ) // forwards
              {
              empty.transform.Rotate(Vector3.right, turnSpeed * Time.deltaTime);
          }         
              else if (Input.GetAxis("Mouse ScrollWheel") < 0f ) // backwards
                  {
              empty.transform.Rotate(Vector3.right, -turnSpeed * Time.deltaTime);
              }
          }
      }
 
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

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

137 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 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 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 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 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 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 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

Why won't my model rotate up on X axis? 1 Answer

Script problem: Trying to make the code of this script run at a set interval (GUI handle object) 1 Answer

Getting an error when trying to add a script to a prefab 0 Answers

disable/enable arrow keys with button,turn 0 Answers

change Volume of Audiomixergroups by script 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