- Home /
Duplicate Question
Can´t get the character to Stay on a moving Platform
I Know that this Question have been asked alot of times... I have searched the web and tryed everything i could find on this topic, but still i cant get it working.
here are my Specs:
Character:
BoxCollider2D, Not set as an Trigger
RigidBody2d, is not kinematic
c# Code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speedForce = 25f;
public Vector2 jumpVector;
public bool isGrounded;
public float playerSpeed;
public bool goesWest;
public float minSwipeDistY;
public Transform grounder;
public float radiuss;
public LayerMask ground;
Animator anim;
//
void OnCollisionStay ( Collision hit) {
if (hit.gameObject.tag == "platform") {
transform.parent = hit.transform;
} else {
transform.parent = null;
}
}
void Start () {
anim = GetComponent<Animator> ();
}
public bool get_playerDirection(){
return goesWest;
}
// Update is called once per frame
void Update () {
// Shoot
for (int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
// -- Tap: quick touch & release
// ------------------------------------------------
if (touch.phase == TouchPhase.Ended && touch.tapCount == 1)
{
{
WeaponScript weapon = GetComponent<WeaponScript>();
if (weapon != null)
{
// false because the player is not an enemy
weapon.Attack(false);
}
}
}
}
// Shoot end
// Android Controll
if (Application.platform == RuntimePlatform.Android) {
// if (Input.GetKey (KeyCode.A)) {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (0).deltaPosition.x < 0) {
playerSpeed = isGrounded ? speedForce : speedForce * 0.7f;
goesWest =true;
rigidbody2D.velocity = new Vector2 (-playerSpeed, rigidbody2D.velocity.y);
transform.localScale = new Vector3 (-0.1f, 0.1f, 1);
anim.SetFloat ("Speed", Mathf.Abs (speedForce));
// } else if (Input.GetKey (KeyCode.D)) {
} else if (Input.GetTouch (0).deltaPosition.x > 0) {
playerSpeed = isGrounded ? speedForce : speedForce * 0.7f;
goesWest =false;
rigidbody2D.velocity = new Vector2 (playerSpeed, rigidbody2D.velocity.y);
transform.localScale = new Vector3 (0.1f, 0.1f, 1);
anim.SetFloat ("Speed", Mathf.Abs (speedForce));
} else if (isGrounded) {
rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y);
anim.SetFloat ("Speed", Mathf.Abs (0));
}
isGrounded = Physics2D.OverlapCircle (grounder.transform.position, radiuss, ground);
// if (Input.GetKey (KeyCode.Space) && isGrounded == true) {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (0).deltaPosition.y > 0 && isGrounded == true) {
rigidbody2D.AddForce (jumpVector, ForceMode2D.Force);
}
} else
// Pc Controlle///////////////////////////////////////////////////////////
{
{
// ...
// 5 - Shooting
bool shoot = Input.GetKeyDown(KeyCode.W);
shoot |= Input.GetKeyDown(KeyCode.W);
// Careful: For Mac users, ctrl + arrow is a bad idea
if (shoot)
{
WeaponScript weapon = GetComponent<WeaponScript>();
if (weapon != null)
{
// false because the player is not an enemy
weapon.Attack(false);
}
}
//
if (Input.GetKey (KeyCode.A)) {
// if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (0).deltaPosition.x < 0) {
playerSpeed = isGrounded ? speedForce : speedForce * 0.7f;
goesWest =true;
rigidbody2D.velocity = new Vector2 (-playerSpeed, rigidbody2D.velocity.y);
transform.localScale = new Vector3 (-0.1f, 0.1f, 1);
anim.SetFloat ("Speed", Mathf.Abs (speedForce));
} else if (Input.GetKey (KeyCode.D)) {
//} else if (Input.GetTouch (0).deltaPosition.x > 0) {
playerSpeed = isGrounded ? speedForce : speedForce * 0.7f;
goesWest =false;
rigidbody2D.velocity = new Vector2 (playerSpeed, rigidbody2D.velocity.y);
transform.localScale = new Vector3 (0.1f, 0.1f, 1);
anim.SetFloat ("Speed", Mathf.Abs (speedForce));
} else if (isGrounded) {
rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y);
anim.SetFloat ("Speed", Mathf.Abs (0));
}
isGrounded = Physics2D.OverlapCircle (grounder.transform.position, radiuss, ground);
if (Input.GetKey (KeyCode.Space) && isGrounded == true) {
//if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (0).deltaPosition.y > 0 && isGrounded == true) {
rigidbody2D.AddForce (jumpVector, ForceMode2D.Force);
}
}
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere (grounder.transform.position, radiuss);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "fire") {
Debug.Log ("You are DEAD");
Application.LoadLevel (1);
}
}
}
MovingPlatform:
BoxColider2D, is not an trigger
RigidBody2d , is kinematic
c# Code:
using UnityEngine;
using System.Collections;
public class MovingPlatform : MonoBehaviour
{
private float useSpeed;
public float directionSpeed = 9.0f;
float origY;
public float distance = 10.0f;
void Start ()
{
origY = transform.position.x;
useSpeed = -directionSpeed;
}
void Update ()
{
if(origY - transform.position.x > distance)
{
useSpeed = directionSpeed; //flip direction
}
else if(origY - transform.position.x < -distance)
{
useSpeed = -directionSpeed; //flip direction
}
transform.Translate(useSpeed*Time.deltaTime,0,0);
}
}
Right, for the moving platform try the following after the Update function:
void OnTriggerEnter2D(Collider2D Coll)
{
Coll.transform.parent = transform;
}
Now go to your platform in you scene view and give the object that has the '$$anonymous$$ovingPlatform' script 2 BoxCollider2D. $$anonymous$$ake on BoxCollider2D as big as the platform (so that you're able to stand on it) and NOT a Trigger and make the other one small in height, but so that it covers the top of the platform, this BoxCollider2D SHOULD be a Trigger. If everything is set up correctly, it should work. Hope this helps!
Answer by Trungdv · Nov 20, 2014 at 01:25 AM
I had the similar problem, but I fixed it. Here is the method: To moving your platform, do not use transform.Translate() in Update(), try using rigidbody2d.movePosition() in FixedUpdate() instead.