- Home /
Player doesnt want to move from moving platform
I have simple moving platform that moves between 2 points with collider2d (trigger). When player enters the trigger he became a child to the platform. Player have collider, rigidbody. Platform script:
void FixedUpdate()
{
if (gameObject.transform.position != target.position)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, Time.fixedDeltaTime * speed);
}
else
{
if(target == end)
{
target = start;
}
else
{
target = end;
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
collision.gameObject.transform.SetParent(transform);
collision.gameObject.transform.position = transform.position;
}
private void OnTriggerExit2D(Collider2D collision)
{
collision.gameObject.transform.SetParent(null);
}
When player hits the platform collider so he can move with the platform. But when i want to player to get off the platform the problem occurs: i have to press the move button 2 times. When i press it for the first time player just the player ceases to be a child and doesn't move with the platform in any direction. When i press it for the second time player finally moves to the destination. How to make player to move to the target with just 1 pressing at the move button? Player script:
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
moveDir = Vector2.up;
inputAvailable = false;
}
// the same for A, S and D
}
private void FixedUpdate()
{
if (inputAvailable == false)
{
transform.SetParent(null);
var temp = rigidbody.position + moveDir;
Debug.Log(temp);
temp.x = Mathf.Round(temp.x); // i want grid movement
temp.y = Mathf.Round(temp.y);
rigidbody.MovePosition(temp);
}
inputAvailable = true;
}
Your answer
Follow this Question
Related Questions
How can I increase or decrease jump speed while keeping the jump arc the same? 1 Answer
Camera Z Position moves to Zero, when a Rigidbody2D is added to a gameobject 0 Answers
,Rotating a 2D sprite with Input 0 Answers
Trying to get actual good arrow physics in 2D 1 Answer
How to check collision on tilemap. 1 Answer