- Home /
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.
What is this public variable collision object?
Why are you calling OnCollisionEnter every fixed update (you shouldn't really ever call it yourself except in very special circumstances)?
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).
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.
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)
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.
Your answer
Follow this Question
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