- Home /
,My child zeros out while idle
I have a Root > Parent > Child, Child animated Prefab. The animator is on the Root and all animations (4 idle and 4 moving) work as intended. However, any object applied to a Child seems to zero out when idle. The children are empty GOs I called Hand_Left & Hand_Right and I've put them inside a parent called Hands which is zero'd out. The issue is while idling, any item I put in the character's "hand" seems to ... ok this is the issue, the player has hands. The item goes to the center of the player when idling. When moving, it works as intended. But when idle, it leaves the hand and finds the center of the player instead.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed = 5f;
public Rigidbody2D rb;
Vector3 change;
private Animator animator;
void Awake()
{
GetAnimator();
GetRigidbody();
}
void GetAnimator()
{
animator = GetComponent<Animator>();
}
void GetRigidbody()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
change.x = Input.GetAxis("Horizontal");
change.y = Input.GetAxis("Vertical");
UpdateAnimationAndMove();
}
void UpdateAnimationAndMove()
{ if (change != Vector3.zero) { MoveCharacter(); animator.SetFloat("moveX", change.x); animator.SetFloat("moveY", change.y); animator.SetBool("moving", true); } else { animator.SetBool("moving", false); } }
void MoveCharacter()
{
rb.MovePosition(transform.position + change * moveSpeed * Time.fixedDeltaTime);
},All of the animations work. I have 4 for moving and 4 for idling. But when Idle, children applied to the animated GO seem to zero out. The prefab is setup as follows:
Root > Parent > Child, Child (we'll call these hands).
This is a 2D character. When I put an object in his hands, it goes to the Root's 0,0 position and not the Hands 0,0 position, but only when idle. When moving, the item acts how it's supposed to.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed = 5f;
public Rigidbody2D rb;
Vector3 change;
private Animator animator;
void Awake()
{
GetAnimator();
GetRigidbody();
}
void GetAnimator()
{
animator = GetComponent<Animator>();
}
void GetRigidbody()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
change.x = Input.GetAxis("Horizontal");
change.y = Input.GetAxis("Vertical");
UpdateAnimationAndMove();
}
void UpdateAnimationAndMove()
{ if (change != Vector3.zero) { MoveCharacter(); animator.SetFloat("moveX", change.x); animator.SetFloat("moveY", change.y); animator.SetBool("moving", true); } else { animator.SetBool("moving", false); } }
void MoveCharacter()
{
rb.MovePosition(transform.position + change * moveSpeed * Time.fixedDeltaTime);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612235350im_/https://answers.unity.com/themes/thub/images/avi.jpg)