Problem with Circle moving platform

Updated on July 20, 2018 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on July 20, 2018

i have problem with circle moving platform
i have Circle the circle rotate around itself and move right and left
when player jump on it the player be child of the Circle
it works fine but sometime doesn’t (not be a child of a circle)

Circle attached with PlatformController
Player attached with PlayerController

PlatformController.cs

 
using UnityEngine;
using System.Collections;
 
public class PlatformController : MonoBehaviour {
 public int rotateSpeed;
 public int moveSpeed;
 public bool isMoving;
 void Start () {
 rotateSpeed = Random.Range (1,7);
 moveSpeed = Random.Range (1,7);
 }
 void FixedUpdate () {
 transform.Rotate (0,0,1);
 if (isMoving) { 
 transform.Translate (moveSpeed * Time.deltaTime ,0,0 , Space.World);
 }
 }
 void OnTriggerEnter2D (Collider2D other) {
 if (other.gameObject.tag == "rightBorder") {
 moveSpeed = -moveSpeed;
 }
 if (other.gameObject.tag == "leftBorder") {
 moveSpeed = Mathf.Abs (moveSpeed);
 }
 }
}
 

PlayerController.cs

 
using UnityEngine;
using System.Collections;
 
public class PlayerController : MonoBehaviour {
 
Rigidbody2D rb;
 public float jumpSpeed;
 public bool touchGround;
 
// Use this for initialization
 void Start () {
 rb = GetComponent();
 }
 // Update is called once per frame
 void Update () {
 Jump ();
 }
 public void Jump () {
 if (Input.GetKeyDown (KeyCode.Space)) {
 if (touchGround) {
 rb.constraints = RigidbodyConstraints2D.None;
 rb.velocity = new Vector2 (0, jumpSpeed);
 }
 }
 }
 void OnCollisionEnter2D (Collision2D other) {
 if (other.gameObject.tag == "ground") {
 touchGround = true;
 this.transform.parent = other.gameObject.transform;
 rb.constraints = RigidbodyConstraints2D.FreezePositionY;
 }
 }
 void OnCollisionExit2D (Collision2D other) {
 if (other.gameObject.tag == "ground") {
 touchGround = false;
 this.transform.parent = null;
 rb.constraints = RigidbodyConstraints2D.None;
 }
 }
}
 

anyone no why the player sometime in the game can’t be child of the circle ?
and thanks 
sorry for my english

  • Liked by
Reply