- Home /
Resetting Ball Object to the original parent and position
So I am trying to reset a ball to its initial position and re-attach it to its original parent. I am learning coding and I am creating a mini baseball game, not for release, just for me to practice getting objects to do what I want. I have the ball successfully coming out of the pitchers hand (dropping from its parent which is the hand) and being pitched. I can successfully hit it. I have a collision set up that when the ball reaches the outfield it resets to initial state. What is happening is the ball looks like its a couple feet away from the pitchers hand after it resets, the pitcher still throws another pitch and I can hit it again, but it is always a couple feet away from the hand after the first pitch. Here is the part of the code that seems to not be working correctly, any help on what I am doing wrong?
public class LaunchBall : MonoBehaviour
{// launch variables
[SerializeField] private Transform TargetObjectTF;
[SerializeField] private Transform TargetHitTF;
[Range(10.0f, 75.0f)] public float LaunchAngle;
public GameObject Hitter;
public GameObject Pitcher;
public GameObject empty;
public GameObject ball;
// cache
private Rigidbody rigid;
private Vector3 initialPosition;
private Quaternion initialRotation;
//-----------------------------------------------------------------------------------------------
// Use this for initialization
void Start()
{
rigid = GetComponent<Rigidbody>();
transform.parent = empty.transform;
initialPosition = transform.position;
initialRotation = transform.rotation;
}
// resets the ball, batter, and pitcher to its initial position
void ResetToInitialState()
{
LaunchAngle = 30.0f;
GetComponent<Rigidbody>().useGravity = false;
Pitcher.GetComponent<Pitcher>().ResetPitcher();
transform.parent = empty.transform;
transform.SetPositionAndRotation(initialPosition, initialRotation);
Hitter.GetComponent<SwingScript>().ResetBatter();
rigid.velocity = Vector3.zero;
}
Answer by unity_XnvR_07NFIOPjA · Nov 11, 2019 at 03:03 AM
I got it figured out. needed to use localposition instead.
Answer by bucketman5000 · Sep 12, 2021 at 11:47 AM
Hey could you explain what you did to figure this out? I also need to reset the exact pos and rot of my item back within its original parent
Your answer
Follow this Question
Related Questions
my prefab is null woe 2 Answers
Coroutine - transform for every frame for duration? 0 Answers
Changing transform.parent moved the position of the parent to the position of the child gameobject. 0 Answers
trying to take my door script and apply to a treasure chest 0 Answers
Set Procedural Cave Generation as child at parent location? 0 Answers