rigidbody2d on a moving stage
Hello everyone
I am working on a 2D game and i have rigidbody which is going to move with a moving stage. The problem is that if the object is not the child of the stage it fall of the stage but i can't make in a child because it is a rigidbody2D.
(C#)
what should i do? thanks for the help
Answer by oren_danniel · Aug 10, 2016 at 12:09 PM
Never mind I have made my own code(C#) to solve the problem . Do let me know if there is any problems or things you want me to change in the code, but if you know how to solve the problem do let me know. thanks for the help.
using UnityEngine; using System.Collections;
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
Mirror Networking Moving platform jittering 0 Answers
2d Moving Platforms 1 Answer
Rigidbody (player) moves slower on moving platform 2 Answers
Stop rigid bodies from sinking when standing on it. 1 Answer
All colliders not longer working 0 Answers