Hello there im new to unity. I’m currently having a problem where I want the Player when he hits the ball of the game the ball tranform to his hands. I know that I need to use void OntriggerEnter() but this script is going to be attach to the Player and not the ball is it possible to do that? This is my currently code attach to the Player.
public GameObject player;
public GameObject ball;
public GameObject SpawnBallPlayer;
private Rigidbody kinematic;
public Transform transformBall;
public Transform transformPlayer;
// Use this for initialization
void Start()
{
kinematic = GetComponent<Rigidbody>();
}
void OnTriggerEnter(Collider other)
{
Debug.Log(other.name + " : " + other.tag);
if (other.tag == "Ball")
{
transformBall = other.transform;
transformBall.transform.parent = player.transform.parent;
transformBall.position = SpawnBallPlayer.transform.position;
ball.GetComponent<Collider>().enabled = false;
ball.GetComponent<Rigidbody>().isKinematic = true;
}
OnTriggerEnter/Stay/Exit is broadcasted to the object with the trigger component and the object(s) with the rigidbody and collider components that are triggering it, so you can put the function in a script attached to any of those objects, no problem.
As for the attaching, you’ll probably want to do this using Transform.SetParent(). If using a rigged model, you’ll want to reference the hand-bone’s transform specifically, so just make a public Transform member in your script there, then drag the hand bone’s GameObject into that slot in the inspector. If the model has been “optimized”, the bones are mostly hidden so you’ll need to go specifically expose the transform in the model for the hand bone so that you can use its position data. In the OnTriggerEnter, you’ll just set the ball’s transform to be parented to the hand’s transform, and then it’ll “stick” to it and move around with the hand.
You may want to set the optional “worldPositionStays” parameter for SetParent to False, so that it zeroes out the localposition/rotation/scale values when it re-parents the ball so it’s not sort of floating in the air nearby, but actually overlapping the hand bone. You can make little x/y/z offsets so that it looks to be resting in the hand properly instead of overlapping it, but that you’ll have to do manually, just eye-balling it.
Hi there Lysander, Thank You for the kind reply and taking you’re time to reply. So for example if I want to make the Player to be parent of Ball all I need to do is :
Just a quick note. Check this out for posting code:
But if you set up your code in your last reply then this:
is actually not necessary.
If this script is on the Player than just typing transform.Something will be the transform of the Player
Also you OnTriggerCollider has the gameObject information in it:
Hi tatatok, my apoligies I have fix the codes. One more quesion so I managed to get the ball to be child of the player by pressing T but is there a way for him to releases the ball if you press a Key. This is my current code for picking
OnTriggerEnter is an event of sorts that happens in a single frame the exact moment that a rigidbody/collider crosses the threshold into a trigger area. In that single frame, it broadcasts “OnTriggerEnter” to the objects directly affected by the collision. Similarly, GetKeyDown/GetButtonDown happens in a single frame when the given button is pressed, and only in that frame. The chance of those two events occurring on the same frame is negligible, so you’ll need to put OnKeyDown either in OnTriggerStay (is broadcasted every frame that an object with a rigidbody/collider is inside of a trigger area) or Update (runs every frame) so that you’re sure to catch the button press.