My math skills have once again come to haunt me. I am building a vertical platformer for mobile devices where the character jumps up onto platforms, trying to get as high as possible while avoiding hazards. I have it set up so that the camera doesn't move, instead the platforms and the character move downwards every time a collision happens between the character and a platform. I cannot figure out how to move the platform that is currently being collided with and the player down to the target location which is -4.0 on the Y-axis in the Main Camera GameObject. Here is the code I have to move the character/platforms down to the target position:
/*********************************** Update **********************************/
void Update(){
if (platformMove == true) {
PositionChanging ();
StartCoroutine(WaitToMove());
}
}
/******************** Platform Holder Position Changing **********************/
void PositionChanging(){
Vector2 positionA = new Vector2 (platformHolder.transform.position.x, -4);
newPosition = positionA;
platformHolder.transform.position = Vector2.Lerp(platformHolder.transform.position, newPosition, Time.deltaTime * smooth);
}
//************************ Collision Function ********************************/
void OnCollisionEnter2D(Collision2D coll)
{
foreach(ContactPoint2D contact in coll.contacts)
{
platformMove = true;
currentPlatformPosition = (coll.transform.position.y);
}
}
}
//********************** Wait to Set platformMove to false *******************/
IEnumerator WaitToMove(){
yield return new WaitForSeconds(0.5f);
platformMove = false;
}
As you can see I am assigning the current collision position to the currentPlatformPosition in the OnCollision2D function but I haven't done anything with it yet. I pretty sure I should be using it somewhere! I've tried a million different things and got close a few times but I can't seem to nail this down.
When platforms are instantiated they are placed inside of the Platform_Holder GameObject. My main character is also in there so he moves with the platforms when I re-position Platform_Holder.
Here is a visual representation:
void Update(){
if (platformMove == true) {
PositionChanging ();
}
}
void PositionChanging(){
StartCoroutine(WaitToMove());
Vector2 positionA = new Vector2 (platformHolder.transform.position.x, platformHolder.transform.position.y - 4);
newPosition = positionA;
platformHolder.transform.position = Vector2.Lerp(platformHolder.transform.position, newPosition, Time.deltaTime * smooth);
}
void OnCollisionEnter2D(Collision2D coll)
{
foreach(ContactPoint2D contact in coll.contacts)
{
platformMove = true;
currentPlatformPosition = (coll.transform.position.y);
}
}
IEnumerator WaitToMove(){
yield return new WaitForSeconds(0.5f);
platformMove = false;
}
What I did: 1) Moved your routine call into the method and outside of the loop so that it isn't being continuously called while the platform is moving 2) Changed the hard -4 to the actual y position minus 4 3) Removed an extra bracket (accident?)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.