1
\$\begingroup\$

I'm currently working on a geometry dash like game, except it is a triangle instead of a square (rubrub calls it a cube). I want it so that it rotates back to 0, 0, 0, when it is touching the ground. Except, there is a sort of jitter when it reaches the ground. Making the collider a square instead of a triangle seemed to fix it slightly, but it still doesn't completely work. A gif of what is happening:

Jitter when triangle reaches ground

The gif is a low fps, but you'll notice it sort of moves around when it reaches the ground. What I want to happen is that as soon as it reaches the ground it goes back to the position at the end of the gif. Here's the code to my jump script: (that also makes it go to the regular rotation when touching the ground)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Jump : MonoBehaviour
{
    Rigidbody2D rb;

    public bool touchingGround;

    public float zRotation;

    public float jumpForce;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
        if (touchingGround)
        {
            zRotation = 0f;
        }
        else
        {
            zRotation += Time.deltaTime * -150f;
        }

        transform.rotation = Quaternion.Euler(0f, 0f, zRotation);
        transform.position = new Vector3(0f, transform.position.y, 0f);

        if (Input.GetKey(KeyCode.Space) && touchingGround == true)
        {
            Bounce();
        }
    }

    void Bounce()
    {
        rb.AddForce(new Vector2(0f, jumpForce * 25f), ForceMode2D.Impulse);
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        touchingGround = true;
    }

    void OnCollisionExit2D(Collision2D col)
    {
        touchingGround = false;
    }
}
\$\endgroup\$
5
  • 1
    \$\begingroup\$ If you have a physics body on something, you should consider transform.position and transform.rotation as being read-only, and move/orient the object exclusively with its physics body. \$\endgroup\$
    – DMGregory
    Commented Feb 10 at 20:23
  • \$\begingroup\$ It doesn't auto align when it touches the ground, because it needs to move its x-axis, which must be locked in place (if I don't it goes flying), and it doesn't work. \$\endgroup\$ Commented Feb 10 at 20:54
  • \$\begingroup\$ What specifically does not work? What code did you write, and what was the result? How did the result differ from what you want? \$\endgroup\$
    – DMGregory
    Commented Feb 10 at 21:02
  • \$\begingroup\$ Have you tried just turning down the zrotation amount so it stops overrotating. And also the jitter could be fixed by setting your rigidbodys interpolate mode setting "interpolate" \$\endgroup\$
    – Pow
    Commented Feb 11 at 11:42
  • \$\begingroup\$ What happens if you don't rotate it manually and just let the physics engine settle it into place? I would expect it to immediately settle into a flat position. \$\endgroup\$
    – Kevin
    Commented Feb 14 at 19:34

0

You must log in to answer this question.

Browse other questions tagged .