[英]How to move my GameObject to desired position?
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.
我无法弄清楚如何将当前正在与之碰撞的平台以及玩家下移到主摄像机GameObject中Y轴上的-4.0的目标位置。 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. 如您所见,我正在将当前碰撞位置分配给OnCollision2D函数中的currentPlatformPosition,但我还没有做任何事情。 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. 实例化平台后,将它们放置在Platform_Holder GameObject内部。 My main character is also in there so he moves with the platforms when I re-position Platform_Holder.
我的主要角色也在其中,因此当我重新放置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?) 我做了什么:1)将例行调用移到方法中,并移至循环外部,以免平台移动时不被连续调用2)将硬-4更改为实际y位置减去4 3)删除了多余的括号(意外?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.