Hello to everyone.I have a problem on my 2D game.I made a moving platform but when the player is on the platform slips and i want to move player and platform together.I know that is a common question.I try several things that i found on internet but with no result.Please help me.
my player has a rigidbody, box and circle collider and
my moving platform has 2 box collider and platform effector 2D.
My player script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float maxSpeed;
public Vector3 respawnPoint;
bool grounded = false;
float groundCheckRadius = 0.2f;
public LayerMask groundLayer;
public Transform groundCheck;
public float jumpHeight;
Rigidbody2D myRB;
Animator myAnim;
bool facingRight;
public Transform gunTip;
public GameObject bullet;
float fireRate = 0.5f;
float nextFire = 0f;
// Use this for initialization
void Start () {
myRB = GetComponent<Rigidbody2D> ();
myAnim = GetComponent<Animator> ();
respawnPoint = transform.position;
facingRight = true;
}
// Update is called once per frame
void Update(){
if (grounded && Input.GetAxis ("Jump") > 0) {
grounded = false;
myAnim.SetBool ("isGrounded", grounded);
myRB.AddForce (new Vector2 (0, jumpHeight));
}
if (Input.GetAxisRaw ("Fire1") > 0) fireRocket ();
}
void FixedUpdate () {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, groundLayer);
myAnim.SetBool ("isGrounded", grounded);
myAnim.SetFloat ("VerticalSpeed", myRB.velocity.y);
float move = Input.GetAxis ("Horizontal");
myAnim.SetFloat ("speed", Mathf.Abs (move));
myRB.velocity = new Vector2 (move * maxSpeed, myRB.velocity.y);
if (move > 0 && !facingRight) {
flip ();
} else if (move < 0 &&facingRight) {
flip ();
}
}
void flip(){
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *=-1;
transform.localScale = theScale;
}
void fireRocket(){
if (Time.time > nextFire) {
nextFire = Time.time + fireRate;
if (facingRight) {
Instantiate (bullet, gunTip.position, Quaternion.Euler (new Vector3 (0, 0, 0)));
} else if (!facingRight) {
Instantiate (bullet, gunTip.position, Quaternion.Euler (new Vector3 (0, 0, 180f)));
}
}
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "cleaner") {
transform.position = respawnPoint;
}
if (other.tag == "Checkpoint") {
respawnPoint = other.transform.position;
}
}
}
My moving platform script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingPlatform : MonoBehaviour {
public float moveSpeed;
public Transform currentPoint;
public Transform[] points;
public int pointSelection;
public bool goup = false;
// private Animator anim;
// public bool spin = false;
void Start ()
{
currentPoint = points[pointSelection];
//anim = gameObject.GetComponent<Animator>();
}
void OnTriggerEnter2D (Collider2D col)
{
if(col.tag=="Player")
goup = true;
}
void OnTriggerStay2D(Collider2D col)
{
if(col.tag=="Player")
goup = true;
// anim.SetBool("Spin", spin=true);
}
void OnTriggerExit2D (Collider2D col)
{
if(col.tag=="Player")
goup = false;
// anim.SetBool("Spin", spin = false);
}
void FixedUpdate()
{
if (goup == true)
{
currentPoint = points[1];
transform.position = Vector3.MoveTowards(transform.position, currentPoint.position, Time.deltaTime * moveSpeed);
if (transform.position == currentPoint.position)
{
pointSelection++;
if (pointSelection == points.Length)
{
pointSelection = 0;
}
currentPoint = points[pointSelection];
//anim.SetBool("Spin", spin = false);
}
}
else if (goup == false)
{
currentPoint = points[0];
transform.position = Vector3.MoveTowards(transform.position, currentPoint.position, Time.deltaTime * moveSpeed);
}
}
}
Please help me i don’t know what to do.