- Home /
Character on a platform in motion
hello, I wrote this script so that a character can be on a platform in motion but it does not work and I have no error message. it's already 2h that I'm looking on the internet without finding so you have an idea because I'm short idea there ...
my piece of script attach to a character
public void OnCollisionStay(Collision col)
{
if (col.gameObject.tag == "Plateform")
{
col.gameObject.GetComponent<Transform>().parent = col.gameObject.GetComponent<Transform>();
}
else
{
col.gameObject.GetComponent<Transform>().parent = null;
}
}
Answer by tormentoarmagedoom · May 23, 2019 at 09:49 AM
Hello.
You are doing it wrong. First i understand this script is attached to the player.
You need to make the player CHILD of the platform
>Platform
>Player
But in this code you are doing the oposite... You need to make the platform the parent of the player, and i understand this script is in the player, (because you use "col.gameObject.tag == "Plateform").
public void OnCollisionStay(Collision col)
{
if (col.gameObject.tag == "Plateform")
{
col.gameObject.GetComponent<Transform>().parent=col.gameObject.GetComponent<Transform>();
}
else
{
col.gameObject.GetComponent<Transform>().parent = null;
}
}
Bye!
In fact it just miss defining "col" as parents of "player" and define a GameObject for the player
public GameObject Player;
public void OnCollisionStay(Collision col)
{
Player.transform.parent = col.transform;
if (col.gameObject.tag == "Plateform")
{
col.gameObject.GetComponent<Transform>().parent = col.gameObject.GetComponent<Transform>();
}
else
{
col.gameObject.GetComponent<Transform>().parent = null;
}
}