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 Hedonsoft · Sep 21, 2014 at 07:28 PM · raycastpickup

Using Raycast to pickup weapon, but not it's not moving with player

I'm using Raycasting to pickup a weapon on button click. When I do the weapon is parented to the RightHand bone but when I move the weapon doesn't move with me. Not sure what I'm doing wrong.

  using UnityEngine;
 using System.Collections;
 using NN.UMAScripts;
 
 public class HoldWeapons : MonoBehaviour {
 
     public GameObject weaponToHold;
     public GameObject hand;
     public GameObject body;
 
     // Use this for initialization
     void Start () {
         hand = GameObject.Find("RightHand");
         body = GameObject.Find("MyPlayer");
     }
     
     // Update is called once per frame
     void Update () {
         PickupWeapon();
     }
 
     void PickupWeapon()
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         RaycastHit hit;
         Debug.DrawRay(ray.origin, ray.direction * 10, Color.red);
 
         if (Input.GetMouseButtonDown(0))
         {
             if (Physics.Raycast(ray, out hit) == true&&hit.collider.tag=="Pickup.Weapon")
             {
                 weaponToHold = hit.collider.gameObject;
                 weaponToHold.transform.parent = hand.transform;
                 weaponToHold.transform.localPosition = new Vector3(0, 0, 0);
                 weaponToHold.transform.rotation = new Quaternion();
                 Debug.Log(weaponToHold.name);
             }
         }
     }
 }
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 dmg0600 · Sep 21, 2014 at 07:40 PM

Next time please try to put some order in your code, it is really difficult to read if it is not well spaced.

You are not parenting it to the hand, is the only thing you are missing. Use transform.parent like this:

 public GameObject weaponToHold;
 public GameObject hand;
 public GameObject body;
 
 // Use this for initialization
 void Start () {
     hand = GameObject.Find("RightHand");
     body = GameObject.Find("MyPlayer");
 }       
     
 // Update is called once per frame
 void Update () {
     PickupWeapon();
 }
 
 void PickupWeapon()
 {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
     RaycastHit hit;
          
     if (Input.GetMouseButtonDown(0))
     {
         if (Physics.Raycast(ray, out hit) == true && hit.collider.tag=="Pickup.Weapon")
         {
             weaponToHold = hit.collider.gameObject;
             weaponToHold.transform.parent = hand.transform;
             Debug.Log(weaponToHold.name);
         }
     }
 }
Comment
Add comment · Show 4 · 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 Hedonsoft · Sep 21, 2014 at 07:46 PM 0
Share

I updated my code and fixed the formatting. It's still not working. The RightHand object is being assigned correctly to hand, and the weaponObject initially looks like it gets parented to the hand but as soon as i move the weapon doesn't mov.

avatar image dmg0600 · Sep 21, 2014 at 07:50 PM 0
Share

Have you seen in the hierarchy that it is actually being parented to the hand?

Does the waepon script have some code to force its position?

avatar image Hedonsoft · Sep 21, 2014 at 07:51 PM 0
Share

No actually I don't have a script on the weapon yet

avatar image Hedonsoft · Sep 21, 2014 at 07:54 PM 0
Share

Ok restarted Unity and it works now:)

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

25 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

Related Questions

Picking up rigidbody objects 2 Answers

Raycasting to pick up an item problem 1 Answer

[Problem] Cant activate 2D objects with touch? 1 Answer

Problems with raycast 1 Answer

Problem With Raycast Pickup Using Touch 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