2d Moving Platforms
okay so I know this has been asked before but it seems like the other implementations are different from my own. Im working on a basic platformer and im having some issues with moving platforms. I've tried parenting, and im currently playing with directly setting the velocity when they are touched. I added a second collider to my player so that it will only check on his bottom but i cant get the velocity to transfer correctly.
Here is how my player object is set-up
and heres my platform
but when it runs depending on which implementation i use one of two things happens.
if i parent then the player remains completely stationary
if i set velocity the player moves at a different speed from the platform, but the velocity shows up as being the same between the two.
here is my code so far.
character controller
using UnityEngine;
using System.Collections;
public class characterController : MonoBehaviour {
//axis values
public float horizontal = 0;
public float vertical = 0;
public float shoot = 0;
public float moveMultiplier = 1;
public float jumpTime = 3;
public float jumpSpeed = 1;
public float jumpCounter = 0;
public bool jumping = false;
Rigidbody2D phy;
// Use this for initialization
void Start ()
{
phy = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update ()
{
//Debug.Log("player Velocity: " + Time.fixedTime + " - " + GetComponent<Rigidbody2D>().velocity);
horizontal =Input.GetAxis("horizontal");
vertical = Input.GetAxis("vertical");
//movement
phy.velocity = new Vector2((horizontal * moveMultiplier), phy.velocity.y);
//jumping
if (!jumping)
{
if (vertical != 0 && jumpCounter<jumpTime)
{
phy.velocity = new Vector2(phy.velocity.x, jumpSpeed * vertical);
jumping = true;
}
}
else
{
if (vertical != 0 && jumpCounter < jumpTime)
{
phy.velocity = new Vector2(phy.velocity.x, jumpSpeed * vertical);
}
jumpCounter += Time.deltaTime;
}
if(phy.velocity.y==0)
{
jumping = false;
jumpCounter = 0;
}
}
}
Platform code
using UnityEngine;
using System.Collections;
public class velMovingPlatform : MonoBehaviour {
public GameObject startPoint;
public GameObject endPoint;
public Vector2 distance;
public float time=5;
public Vector2 velocityReq;
public int phase = 0;
public float waitTime = 3;
public float timer=0;
// Use this for initialization
void Start ()
{
Vector2 startPos = startPoint.transform.position;
Vector2 endPos = endPoint.transform.position;
distance = new Vector2(Mathf.Abs(startPos.x - endPos.x), Mathf.Abs(startPos.y - endPos.y));
velocityReq = new Vector2(distance.x / time, distance.y / time);
}
// Update is called once per frame
void Update ()
{
//log player velocity
//Debug.Log("platform Velocity: " + Time.fixedTime + " - " + GetComponent<Rigidbody2D>().velocity);
switch (phase)
{
case 0:
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
timer += Time.deltaTime;
timer = Mathf.Clamp(timer, 0, waitTime);
if(timer==waitTime)
{
phase = 1;
timer = 0;
}
break;
case 1:
timer += Time.deltaTime;
timer = Mathf.Clamp(timer, 0, time);
if (timer == time)
{
phase = 2;
timer = 0;
}
else
{
GetComponent<Rigidbody2D>().velocity = velocityReq;
}
break;
case 2:
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
timer += Time.deltaTime;
timer = Mathf.Clamp(timer, 0, waitTime);
if (timer == waitTime)
{
phase = 3;
timer = 0;
}
break;
case 3:
timer += Time.deltaTime;
timer = Mathf.Clamp(timer, 0, time);
if (timer == time)
{
phase = 0;
timer = 0;
}
else
{
GetComponent<Rigidbody2D>().velocity = -velocityReq;
}
break;
}
}
}
Platform Controller
using UnityEngine;
using System.Collections;
public class movingPlatformController : MonoBehaviour {
public GameObject player;
public bool locked = false;
public Vector3 oldpos;
public Vector3 newpos;
public Vector3 difference;
public GameObject othercollider;
void OnTriggerEnter2D(Collider2D other)
{
othercollider = other.gameObject;
//Debug.Log("colEnter layer: " + other.gameObject.layer);
if (other.gameObject.layer == 8)
{
// player.transform.SetParent(other.transform);
}
}
void OnTriggerStay2D(Collider2D other)
{
if(other.gameObject.layer==8)
{
player.GetComponent<Rigidbody2D>().velocity = other.GetComponent<Rigidbody2D>().velocity;
Debug.Log(player.GetComponent<Rigidbody2D>().velocity + " / " + player.GetComponentInParent<Rigidbody2D>().velocity);
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.layer == 8)
{
// player.transform.parent = null;
locked = false;
}
}
}
For the life of me i cant figure it out. Any Ideas?
Answer by oren_danniel · Sep 12, 2016 at 07:47 PM
First, parenting a rigidbody can cause some problems. now I know how to work around the parenting using a script that will give you the same affect without parenting the object, if i understand correctly you want the object to follow the stage?
the script will ensure that the object on the stage will stay at the same place on the stage as the it moves.
any how this is the script:
public class rigidMoveStage : MonoBehaviour { public float Xdistance; public float Ydistance; public Vector2 vector; public Vector2 stageVector; public bool colBool = false;
//this code needs to be on the colliding object
//do let me know if there is any problem
// Update is called once per frame
void Update () {
GameObject colObject = GameObject.Find("col");
if (colBool == true) {
//without gravity(locks y axis)
this.gameObject.transform.position = new Vector2(stageVector.x + Xdistance, stageVector.y + Ydistance);
//with gravity(unlocks y axis)
//this.gameObject.transform.position = new Vector2(stageVector.x + Xdistance, this.gameObject.transform.position.y);
stageVector = colObject.gameObject.transform.position;
}
}
void OnCollisionEnter2D(Collision2D col) {
vector = transform.position;
stageVector = col.gameObject.transform.position;
col.gameObject.name = "col";
Xdistance = vector.x - stageVector.x;
Ydistance = vector.y - stageVector.y;
colBool = true;
//feel free to use it
}
}
Your answer
Follow this Question
Related Questions
(2D) Rigidbody slides a bit through Box Collider 1 Answer
FixedUpdate or Update for collision detection and climbing slopes? 1 Answer
Rigidbody (player) moves slower on moving platform 2 Answers
No overload for method. (Question) 0 Answers
How to move a Rigidbody2D along another Rigidbody2D 1 Answer