• 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 RVDL_IT · May 17, 2017 at 04:08 PM · c#axisx-axis

How to make the vertical axis relative.

I have a pick up and hold items script and the object I'm carrying is moved on the horizontal axis but stays on the same level on the vertical axis. How do I fix this? The object is teleported to a hand empty which doesn't move on the axis.

This is how I hold it correctly: alt text

And this is how I hold it when I look down: alt text

I'd like to be able to hold it in like the first picture when looking up and down.

This is my script:

 using UnityEngine;
 using System.Collections;
 
 #pragma warning disable 0414
 
 namespace CarryingItems {
 
     public class PickUp : MonoBehaviour {
         
         private float RayCastLength = 2.15F;
         public LayerMask RayMask;
         
         private float OffsetDropPosition = 2;
         
         private GameObject UtilityProps;
         private GameObject CarryableObject;
         private GameObject Hand;
         private GameObject Character;
         private GameObject CameraMain;
         private GameObject Handle;
         private GameObject HandleExtra;
         private GameObject BladeHolder;
         private GameObject Blade;
         
         private bool ItemPickedUp;
         
         void Awake() {
             UtilityProps = GameObject.Find("UtilityProps");
             CarryableObject = this.gameObject;
             Hand = GameObject.Find("ItemHand");
             Character = GameObject.Find("PlayerCharacter");
             CameraMain = GameObject.Find("MainCamera");
             Handle = this.gameObject.transform.GetChild(0).gameObject;
             HandleExtra = this.gameObject.transform.GetChild(1).gameObject;
             BladeHolder = this.gameObject.transform.GetChild(2).gameObject;
             Blade = this.gameObject.transform.GetChild(3).gameObject;
         }        
         
         void Start() {
             ItemPickedUp = false;
         }
         
         void Update() {
             if (Input.GetKeyDown("e")) {    
                 if(ItemPickedUp == false) {
                     ItemPickUp();
                 }
                 else if(ItemPickedUp == true){
                     ItemDrop();
                 }
             }            
         }
         
         void ItemPickUp() {
             if (Physics.Raycast(CameraMain.transform.position, CameraMain.transform.forward, RayCastLength, RayMask.value)) {
                 CarryableObject = this.gameObject;
                 CarryableObject.transform.parent = Character.transform;
                 CarryableObject.GetComponent<Rigidbody>().useGravity = false;
                 CarryableObject.GetComponent<Rigidbody>().isKinematic = true;
                 CarryableObject.transform.position = Hand.transform.position;
                 CarryableObject.transform.rotation = Hand.transform.rotation;
                 ItemPickedUp = true;
                 
                 if(Handle != null){
                     Handle.GetComponent<Collider>().enabled = false;
                 }
                 if(HandleExtra != null){
                     HandleExtra.GetComponent<Collider>().enabled = false;
                 }
                 if(BladeHolder != null){
                     BladeHolder.GetComponent<Collider>().enabled = false;
                 }
                 if(Blade != null){
                     Blade.GetComponent<Collider>().enabled = false;
                 }
             }
         }
         
         void ItemDrop() {
             CarryableObject.transform.parent = null;
             CarryableObject.transform.position = CameraMain.transform.position + CameraMain.transform.forward*OffsetDropPosition;
             CarryableObject.transform.rotation = Character.transform.rotation;
             CarryableObject.GetComponent<Rigidbody>().useGravity = true;
             CarryableObject.GetComponent<Rigidbody>().isKinematic = false;
             ItemPickedUp = false;
             
             if(Handle != null){
                 Handle.GetComponent<Collider>().enabled = true;
             }
             if(HandleExtra != null){
                 HandleExtra.GetComponent<Collider>().enabled = true;
             }
             if(BladeHolder != null){
                 BladeHolder.GetComponent<Collider>().enabled = true;
             }
             if(Blade != null){
                 Blade.GetComponent<Collider>().enabled = true;
             }
         }    
     }
 }

PS: Bonus points if you can help me with the fact that I grab 2 items when i try to pick up the cleaver.

pickupcamerawrong.png (161.9 kB)
pickupcameraright.png (111.5 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

Answer by Kristinosis · May 17, 2017 at 04:13 PM

What does your hierarchy and pickup script look like? The easiest way would be to have an empty parented to your camera or whatever moves your character and then have the item you're carrying parented to that empty. Then just keep the item's position at 0,0,0 so it follows the empty.

Comment
Add comment · Show 3 · 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 RVDL_IT · May 17, 2017 at 04:27 PM 0
Share

I have a hand empty where the item spawns to but it doesn't rotate.

I updated my question because I didn't have enough characters to put the script in the comment.

This is my hierarcy: alt text

And I tried to make my hand position relative to the camera but the object turned sideways and I wasn't able to turn it.

hierarcy.png (4.3 kB)
avatar image Kristinosis · May 17, 2017 at 04:42 PM 0
Share

Your ItemHand GameObject follows your Character GameObject correctly? If so try this:

Ins$$anonymous$$d of

 CarryableObject.transform.parent = Character.transform;

try

 CarryableObject.transform.parent = Hand.transform;

And ins$$anonymous$$d of

 CarryableObject.transform.position = Hand.transform.position;

try

 CarryableObject.transform.position = Vector3.zero;

This should make your item always follow the hand, no matter where it goes. Of course, all this will only work if your hand moves in the right place on the camera. You might have to play around with the CarryableObject's rotation to get things looking right, but it should stick in place now at least.

As for the double cleaver, I'm not sure. It would take some more investigation to figure out I think

avatar image RVDL_IT Kristinosis · May 17, 2017 at 04:45 PM 0
Share

The problem is that the hand empty is being moved on the vertical axis. Not just the object. But thank you for your advise. $$anonymous$$y question wasn't very clear about that. Apologies!

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Changing Input.GetAxis camera to cursor movement instead 1 Answer

C# Make an object rotate along axis its moving on 0 Answers

How do you use a Window Shake input? 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