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 Zerosignal_2 · Feb 02, 2014 at 02:50 AM · gameobjectphysicschildparenting

Very Erratic behavior when parenting an object

I have a player object that when touching the ball object can pickup it up. Both have rigid bodies, the collision is detected, the player can pick the object up via parenting using this Player Object(parent)->(child)PlayerHand(empty game object)->(OnCollisionEnter) (parent)->(child of PlayerHand) ball object (local position) But when the ball gets parented it shows that its parented properly BUT instead of sticking to the player object it flies erratically upwards indefinitely never to ACTUALLY touch the player object... using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq;

 public class Targeting : MonoBehaviour
 {
     private Transform myTransform;
     
     Transform closestTarget;
     
     private List<Transform> targets;
     
     private Transform selectedTarget;
     
     private Transform previousTarget;
     
     private int index;
     
     public GameObject Ball;
     
     public GameObject Player;
     
     private float ThrowForce;
     
     public Collision Other;
     
     public GameObject PlayerHand;
     
     
     
     
 
     
     void Start () {
  //instantiating List,Transform,selectedTarget
        myTransform = transform;
  
        selectedTarget = null;
        targets = new List<Transform> ();
        
        AddAllEnemies ();
     }
  
     public void Update() {
       //checking selected target then increasing index size
         if (selectedTarget == null)
              index++;
              if(index > targets.Count) {
               index = 0;
     }
           //calling to target an enemy  
           TargetEnemy ();  
         }
     
     public void AddAllEnemies () {
        //populating list enemy via their transforms
        GameObject[] go = GameObject.FindGameObjectsWithTag ("EnemyCpu");
  
        foreach(GameObject enemy in go)
          AddTarget(enemy.transform);
     }
  
     public void AddTarget (Transform enemy) {
  
        targets.Add(enemy);
     }
     
     private void TargetEnemy () {
  
        SortTargetsByDistance ();
      
        if(closestTarget != targets[0]) {
                   index = 0;
                    closestTarget = targets[0];
               }
        
        if (selectedTarget == null) {
  
          selectedTarget = targets[0];
          SelectTarget ();
        }
  
        else 
        {
          previousTarget = selectedTarget;
          DeselectTarget ();
          TargetEnemy ();
        }
        closestTarget = targets[0];
     }
  
     private void SortTargetsByDistance () {
  
        targets.Sort (delegate(Transform t1, Transform t2) { 
          return (Vector3.Distance (t1.position, myTransform.position).CompareTo(Vector3.Distance (t2.position, myTransform.position)));
        });
     }
  
     private void SelectTarget () {
  
        selectedTarget.renderer.material.color = Color.red;
     }
  
     private void DeselectTarget () {
  
        previousTarget.renderer.material.color = Color.blue;
        selectedTarget = null;
     }
         
     void FixedUpdate()
     {
         OnCollisionEnter(Other);
             TargetEnemy();
               
         if (Input.GetKeyDown("z"))
         {
             Attack(); 
             BallPathing();
         }
             
          if (Input.GetKeyDown("q"))
             {
                 PickUpBall();
             }
      }
 
     void OnCollisionEnter(Collision Other)
     {
         
        GameObject.FindGameObjectWithTag("Ball").GetComponent<Rigidbody>();
        
     }
 
     void Attack()
     {
         Ball.transform.parent = null;
         Ball.rigidbody.isKinematic = false; 
         BallPathing();
     }
 
     void PickUpBall()
     {
        Ball.rigidbody.isKinematic = true;
        Ball.transform.parent = PlayerHand.transform;
        Ball.transform.localPosition = new Vector3(0,0,0);
        
     }
 
     void BallPathing() 
     { 
         ThrowForce = 10f;
 
         Vector3 targetDir = selectedTarget.position - transform.position;
         Vector3 forward = (transform.forward * ThrowForce);
         
         float angle = Vector3.Angle(targetDir, forward);
     }
 }

there are no compiling errors thankfully but for some reason I cannot seem to root this bug out. I'm pretty sure some fresh eyes could find my problem and hopefully clue me in.

Comment
Add comment · Show 2
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 iwaldrop · Feb 02, 2014 at 04:23 AM 0
Share
  1. What is this public variable collision object?

  2. Why are you calling OnCollisionEnter every fixed update (you shouldn't really ever call it yourself except in very special circumstances)?

  3. Why are you getting a component in OnCollisionEnter, but doing nothing with it? Every time the game object that this script is attached to collides with anything you'll be doing a very expensive find game object with tag, then get component, but nothing is done with the component.

I think you should reevaluate how you're doing this. Your method for parenting the ball looks fine, but you can use Vector3.zero ins$$anonymous$$d of new Vector3(0, 0, 0).

avatar image Zerosignal_2 · Feb 02, 2014 at 03:59 PM 0
Share

While I will be the first to admit this is probably not the best way to go, my coding experience is a bit slim. LoL(sigh)As for your questions 1.) simply a collision object nothing more, I figured i was missing something this could be it as i was trying to jam the ball inside of the "Other" object. which if you were to take my code and just have the pickup ball function called during fixed update the ball will attach correctly to the mesh.

  1. This is a game that will only feature 20 to 24 meshes on screen at any one given time. No massive landscapes/huge uber detailed creatures just some decent mid gen WoW low poly models. Also this is going to be a fast paced close quarters kinda game with auto targeting so knowing when and where it hits at all times will be important (for AoE, Shattering,Severing limbs)

  2. I'm sorry, what?!? So you are saying that since I'm getting the rigid-body from the game-object ball, but I've never used it? Secondly it says in the unity code reference guide that any rigid body / physics manipulation should be done inside Fixed Update for accuracy reasons.

In which the collision / picking up / throwing are rigid-body / physic based. Don't get me wrong if i'm heading the wrong way by all means please let me know. Before my last log said that my game consumed 30 mb of ram with 17 out of 20 meshes on screen with only 1 mesh running this targeting script.

this script will end up being run simultaneously in one way or the other across 14 meshes at one time....

so if this will kill my comp at the end of the day lemme know please.

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

Dynamically parent gameobjects 1 Answer

Child rigidbody clipping through walls. 2 Answers

Getting the topmost parent 1 Answer

" You should never have a parent and child rigidbody together " ? 2 Answers

How do I pick up a ragdoll 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