0

I am doing my own "Dinosaur game" (like in Chrome) in Unity. My dinosaur, however, accelerates way too fast. Can you help me to find a problem in my code?

Here the code:

void FixedUpdate()
{
    rb2d.Cast(Vector2.down, hitBuffer);
    float distance = hitBuffer[0].distance;
    if (distance > minMoveDistance)
        Fall();
    else
        Move();
}

void Fall()
{
    transform.Translate(fall);
    fall += Physics2D.gravity * Time.fixedDeltaTime;
    fall = Vector2.ClampMagnitude(fall, 90 * Time.fixedDeltaTime);
}

void Move()
{
    transform.Translate(move);
    move.x += (acceleration * Time.fixedDeltaTime);
}
2
  • Make acceleration a smaller value? Commented Nov 3, 2019 at 0:46
  • Is its fall accelerating too fast or is its move accelerating too fast? Commented Nov 3, 2019 at 3:06

1 Answer 1

1
public void Update()
{
    if (hasAcceleration)
    {
         // AddForce(Vector2 force, ForceMode2D mode = ForceMode2D.Force);
         // 
         rigidbody2D.AddForce(force, ForceMode2D.Force);
    }
}
  • or by your self,
public void AddForce(Vector3 force)
{
    Vector3 f = force;
    f = f / mass;
    acceleration += f;
}

public void AddForce(Vector2 force)
{
    AddForce(new Vector3(force.x, force.y, 0.0f));
}

public void UpdateMovement(float deltaTime)
{
    velocity += acceleration;
    acceleration *= 0;

    movement = velocity * deltaTime;

    transform.localPosition += movement;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.