I’m making a top down 2D game with jump mechanics(graphics are going to look like Chrono Trigger or Terranigma if you’d like to sort of see what I’m seeing).
I’ve decided to use the z value of various objects to control their height. When the player jumps, the Sprite moves along the LocalY axis (this is an animation, basically.) The rigidbody2D controlling the Sprite stays in its shadow, directly underneath. I’ve got the Sprite moving up and down and following the controller correctly, but I want to copy its LocalY value (essentially its height, as the player sees it) to the controller’s Z value (I’ve been casting it to int to make it easier to understand/ use). If the controller’s Z is >= a platform’s, their collisions turn off and the controller can pass. (this way, the controller can fall off the sides, and the platform’s collision turns back on.).
Here’s where it breaks (code is in C#):
private void SetMyZ ()
{
if (isGrounded == true)
{
// this will probably be a Physics2D.OverlapPoint check
// it checks the ground and sets myZ to the ground's Z if I am on the ground
}
else
{
Debug.Log("I am copying my newZ from my Sprite's localY.");
newZ = playerSprite.tellMyHeight;
}
if (newZ != (int)myTransform.position.z)
{
Debug.Log(" I am now setting my Z");
myTransform.position.Set(myBody.position.x, myBody.position.y, newZ);
}
}
When SetMyZ() is called in the FixedUpdate(), it slows the game down and then behaves wildly (the motion is choppy and the jump becomes very high). It’s worth mentioning that the Sprite is on Update().
I think the problem is that using a Rigidbody2D and trying to set transform is slow, but RigidBody 2D won’t handle Z and I need my collision to be 2 Dimensional.
Is the solution handling all control/movement directly with the Transform component? I need collision detection, so I’d rather use a rigidbody2D for that. I’d like to be able to use both methods to freely control my character via scripting, but Transform is looking too slow.
Thanks, I hope you can help me! I’ve got a pretty hard deadline coming up.