I have a 2d animated character. It has a rigidbody some colliders and a movement script.
I have a vertical rising platform and a horizontal moving platform. The parenting method for staying on the platform works fine. But only for one platform. If both platforms are enabled in the scene only one will parent. The horizontal platform parents, but the vertical one will not. If I disable the horizontal platform, then the vertical one works fine. The platforms have a box collider 2D for the collision detection and a empty GO with a collider for the trigger. They are not rigidbodies. I am using iTween for movement. The stay on platform scripts are on the empty GO’s attached to the platforms.
public class vPlatformMove : MonoBehaviour
{
public GameObject gameObject;
void Start()
{
iTween.MoveBy(gameObject, iTween.Hash("y", 8, "speed", 2, "easeType", "easeInOutExpo", "loopType", "pingPong", "delay", .1));
}
}
public class vStayOnPlatform : MonoBehaviour
{
public bool onMovingPlatform = false;
Transform player;
Transform vPlatform;
void Awake()
{
player = GameObject.FindWithTag("Player").transform;
vPlatform = GameObject.FindWithTag ("vPlatform").transform;
}
void Update ()
{
if(onMovingPlatform)
{
player.SetParent (vPlatform);
}
if(!onMovingPlatform)
{
player.SetParent (null);
}
}
void OnTriggerEnter2D(Collider2D other)
{
onMovingPlatform = true;
}
void OnTriggerExit2D(Collider2D other)
{
onMovingPlatform = false;
}
}
public class hPlatformMove : MonoBehaviour
{
public GameObject gameObject;
void Start()
{
iTween.MoveBy(gameObject, iTween.Hash("x", 5, "easeType", "easeInOutExpo", "loopType", "pingPong", "delay", .1));
}
}
public class hStayOnPlatform : MonoBehaviour
{
public bool onMovingPlatform = false;
Transform player;
Transform hPlatform;
void Awake()
{
player = GameObject.FindWithTag("Player").transform;
hPlatform = GameObject.FindWithTag ("hPlatform").transform;
}
void Update ()
{
if(onMovingPlatform)
{
//transform.SetParent (player);
player.SetParent (hPlatform);
}
if(!onMovingPlatform)
{
player.SetParent (null);
}
}
void OnTriggerEnter2D(Collider2D other)
{
onMovingPlatform = true;
}
void OnTriggerExit2D(Collider2D other)
{
onMovingPlatform = false;
}
}
public class PlayerMovement : MonoBehaviour
{
// Movement
public float maxSpeed = 5f;
bool facingRight = true;
//Character Animator
Animator anim;
//Jumping
bool isGrounded = false;
public Transform groundCheck;
float groundRadius = 0.1f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
//Moving platform
//public Transform movingPlatformCheck;
//public bool onMovingPlatform = false;
//public LayerMask whatIsMovingPlatform;
//float movingPlatformCheckRadius = 0.1f;
//Transform platform;
void Awake ()
{
anim = GetComponent<Animator>();
//platform = GameObject.FindWithTag ("Platform").transform;
}
void FixedUpdate ()
{
Movement ();
}
void Update()
{
Jump ();
//StickToPlatform();
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void Movement()
{
//Get the rigidbody component from the player gameobject.
Rigidbody2D myRigidBody2D = GetComponent<Rigidbody2D>();
//Sets the bool "isGrounded" to true when a collision is detected using "OverlapCircle".
isGrounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", isGrounded);
anim.SetFloat ("vSpeed", myRigidBody2D.velocity.y);
//If player is "in the air", does not allow movement.
if(!isGrounded) return;
//Movement.
//float move = Input.GetAxis ("Horizontal");
float move = Input.GetAxis ("LeftAnalog");
anim.SetFloat ("Speed", Mathf.Abs(move));
myRigidBody2D.velocity = new Vector2(move * maxSpeed, myRigidBody2D.velocity.y);
//Flips the sprite to face the correct way.
if(move > 0 && !facingRight)
Flip ();
else if(move < 0 && facingRight)
Flip ();
}
void Jump()
{
Rigidbody2D myRigidBody2D = GetComponent<Rigidbody2D>();
//if(isGrounded && Input.GetKeyDown (KeyCode.Space))
if(isGrounded && Input.GetButtonDown("A"))
{
anim.SetBool ("Ground", false);
myRigidBody2D.AddForce (new Vector2(0, jumpForce));
}
}
}
Thanks for looking.