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 popuppirate · Oct 05, 2014 at 02:30 PM · parentchildforceray

How To Access Children of a Parent Object

Hi all,

I'm trying to build something that looks for the closest gameobject to the player (in this case a weapon) and fulfilling a set of conditions draws a ray between two 'attach points'.

What I've done doesn't seem to work, so how best might I access the children of my known objects 'player' and 'closest_weapon'?

The end result will create a force to drag the weapon to the player, align the gun to the player, and set the gun as a child of the player (much easier than rebuilding the floating physics wow-factor script I did for the main player for each weapon I make).

At the moment the code properly recognises the nearest weapon object, but freezes the player in place with no visible ray being drawn upon setting the boolean to be true. I assume this is because it's getting confused as to where to draw the line or something.

Cheers for your time!

Popupirate

 using UnityEngine;
 using System.Collections;
 
 public class Weapon_Control_Script : MonoBehaviour {
 
     public GameObject closest_weapon; 
     public bool attach_weapon;
     public float distance;
 
 
     // Use this for initialization
     void Start () {
         
 }
     
     // Update is called once per frame
     void Update () {
         if (attach_weapon == false) {
             SearchForWeapons();        
         }
 
         if (attach_weapon == true) {
             AttachWeapon();    
         }
 
 }
 
 
 void AttachWeapon(){ //Look Here!
         Transform p_trans = transform.FindChild ("PlayerAttachPoint"); 
         Transform w_trans = closest_weapon.transform.FindChild ("WeaponAttachPoint");
 
         Vector3 player_attach_vect = new Vector3 (p_trans.position.x, p_trans.position.y, p_trans.position.z);
         Vector3 weapon_attach_vect = new Vector3 (w_trans.position.x, w_trans.position.y, w_trans.position.z);
         Vector3 direction = weapon_attach_vect - player_attach_vect;
         Debug.DrawRay (player_attach_vect, direction, Color.green);
         //closest_weapon.rigidbody.AddForce (-direction * 4);
 }
 
 
 void SearchForWeapons(){ //This works lovely jubly
     GameObject[] weapons = GameObject.FindGameObjectsWithTag ("Weapon");                     
     distance = Mathf.Infinity;                                                            
     for (int i=0; i<weapons.Length; i++) {                                                 
         Vector3 diff_vect = weapons [i].transform.position - transform.position;            
         float diff_abs = Mathf.Abs (diff_vect.sqrMagnitude);                                
         if (diff_abs < distance) {                                                            
         closest_weapon = weapons [i];                                                    
         distance = diff_abs;                                                            
         }
     }
             Debug.Log (closest_weapon.name);
 }
 
 
     
 }
 
Comment
Add comment · Show 4
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 KayelGee · Oct 06, 2014 at 11:55 AM 0
Share

I don't exactly understand what isn't working. Have you tried passing a duration value to DrawRay like this:

 Debug.DrawRay (player_attach_vect, direction, Color.green, 1.0f);

Also you can use

 Vector3 player_attach_vect = p_trans.position;
 

ins$$anonymous$$d of

 Vector3 player_attach_vect = new Vector3 (p_trans.position.x, p_trans.position.y, p_trans.position.z);
         
         
avatar image popuppirate · Oct 06, 2014 at 04:37 PM 0
Share

Hi $$anonymous$$ayelGee, Thanks very much for this reply. The second of your suggestions will be very useful.

$$anonymous$$y problem is that despite the fact the script appears to run correctly, no visible debug ray was drawn between my two objects. I assumed this was because it hadn't found my two child objects. I think you can understand my confusion- the script would tell me something was up if the child objects weren't found and vectors are clearly being generated, but no ray. Also the mouse input stops upon setting the boolean attach_weapon=true in the Inspector.

Hope this provides some context!

Cheers,

Popupirate

avatar image Cherno · Oct 06, 2014 at 04:48 PM 0
Share

$$anonymous$$aybe the ray get drawn only one frame and thus is hard to see.

avatar image popuppirate · Oct 06, 2014 at 05:03 PM 0
Share

Thanks Cherno, but I tried setting the fourth overload variable that was suggested by $$anonymous$$ayelGee of Debug.Drawray to both 1f and 100f with no effect. The mouse input still freezes and no green ray is drawn between the objects in question.

1 Reply

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

Answer by awest · Oct 07, 2014 at 03:36 AM

I see a couple issues. You are calling GameObject.Find every single frame (this is very slow and will cause lag in larger projects) and I don't see where you set attach_weapon to true or false. I would start by altering the script like this to run faster. I also added a Debug after DrawRay to check for expected values and set the fifth argument in DrawRay to show through any foreground. Also just to be sure, you know DrawRay only shows in the scene view, not the game window?

 using UnityEngine;
 using System.Collections;
  
 public class Weapon_Control_Script : MonoBehaviour {
  
 public GameObject closest_weapon;
 public bool attach_weapon;
 public float distance;

 private Transform p_trans;
 private Transform w_trans;
 private GameObject[] weapons;

 void Start () { //I'm assuming these wont change at runtime.
     p_trans = transform.FindChild ("PlayerAttachPoint");
     weapons = GameObject.FindGameObjectsWithTag ("Weapon");
 }
  
 // Update is called once per frame
 void Update () {
     if (attach_weapon == false) {
         SearchForWeapons();
     }
  
     if (attach_weapon == true) {
         AttachWeapon();
     }
 }
  
 void AttachWeapon(){ //Look Here!
     Vector3 player_attach_vect = p_trans.position;
     Vector3 weapon_attach_vect = w_trans.position;
     Vector3 direction = weapon_attach_vect - player_attach_vect;
     Debug.DrawRay (player_attach_vect, direction, Color.green, 1.0f, false);
     //closest_weapon.rigidbody.AddForce (-direction * 4);
     Debug.Log(player_attach_vect+", "+direction);
 }
  
  
 void SearchForWeapons(){ //This works lovely jubly
     distance = Mathf.Infinity;
     for (int i=0; i<weapons.Length; i++) {
         Vector3 diff_vect = weapons [i].transform.position - transform.position;
         float diff_abs = Mathf.Abs (diff_vect.sqrMagnitude);
         if (diff_abs < distance) {
             closest_weapon = weapons [i];
             distance = diff_abs;
         }
     }
     if (w_trans == null || w_trans.parent.name != closest_weapon.name) w_trans = closest_weapon.transform.FindChild("WeaponAttachPoint");
     Debug.Log (closest_weapon.name+" distance: "+distance);
 }
 
 }

I tested this script in a project with only the necessary elements and it worked as expected. Let me know if you have any questions about what I changed. Also, if the weapons are created dynamically, I can suggest alternatives to assigning them at the start.

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 popuppirate · Oct 07, 2014 at 07:07 AM 0
Share

While there is no visible green line being drawn between the objects, it is returning a debug of the vectors which is about the same. I really wanted to put a particle effect along the beam so it looks like a tractor beam or something.

Didn;t realise that I had to keep changing the name of the object w_trans while in the first loop.

You hit the nail on the head as to why i wanted it to update per-frame, since weapons that haven't respawned yet will disappear and reappear in the array. How might I go about doing this dynamically without searching each frame (not that it seemed to hinder my performance too much, but there we go). ## Heading ##

avatar image awest · Oct 10, 2014 at 04:27 AM 0
Share

You can to use the vector3 to update particle velocity so you're on the right track there.

Which ever script you use to respawn/create the weapons can add them to the weapons array. However, since arrays can't be resized it would be easier to code if you use a list.

You can also have an empty object that all weapons are children of. When the player attempts to get a weapon you can get all children's transforms from the empty and cycle through to find the closest. When weapons aren't parented to the player, they can auto parent themselves to the empty.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Detach child and add force 0 Answers

Getting variable data from Parent 1 Answer

How do I find a child object in a hierarchy of children? 1 Answer

How do I allocate a parent to a gameobject that just got instantiated? 2 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