1
\$\begingroup\$

I've come across an odd effect in Unity and would like to understand why it's happening. Using some simple code I've achieved a horizontal movement effect, of a ball across a flat square surface, yet the movement in any given direction is bumpy, as in, it's like the ball is bouncing slowly until it settles in a hole when the key is let up. I've never noticed anyone else's player object doing this, and there doesn't seem to be any instances of it out there, based on my research. One might describe it as "rolling through the inside of an egg carton". Does anyone knows why this is happening, if it's normal, or at the fault of my code?

Here's my code:

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

public class Player : MonoBehaviour
{
    private Rigidbody rb;
    public float speed;

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

    void FixedUpdate()
    {
        var gravity = new Vector3(Input.acceleration.x, Input.acceleration.z, Input.acceleration.y) * 9.8f;
        rb.AddForce(gravity * rb.mass, ForceMode.Acceleration);

        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(horizontal, 0.0f, vertical);
        rb.AddForce(movement * speed);
    }
}

And here's a GIF of what's happening:

enter image description here

\$\endgroup\$
6
  • \$\begingroup\$ Perhaps you can provide a short gif of what's happening? I assume it's that you're applying a large downwards force on the ball and the bouncing is the body bouncing up after having hit the ground plane -- try reducing the "Bounciness" of the physics material you're using. \$\endgroup\$
    – Charly
    Commented Jan 20, 2022 at 13:14
  • 1
    \$\begingroup\$ When you use ForceMode.Acceleration then you don't need to take the mass off the rigidbody into account. Making mass irrelevant is the whole purpose of that option. But that just by the way. \$\endgroup\$
    – Philipp
    Commented Jan 20, 2022 at 13:22
  • \$\begingroup\$ As im rather new to c# @Philipp could you explain exactly how you mean "making mass irrelevant" and what i should change to fix this? \$\endgroup\$
    – Spring
    Commented Jan 20, 2022 at 13:30
  • \$\begingroup\$ As I said this is probably not relevant to your problem. But if you want to know more about force modes, check the documentation. \$\endgroup\$
    – Philipp
    Commented Jan 20, 2022 at 13:31
  • \$\begingroup\$ As Charly mentioned, this could be due to your physics material being too bouncy, causing the ball to bounce along the surface rather than roll. Can you show us the physics material settings you're using for the ball and for the surface it's rolling on? \$\endgroup\$
    – DMGregory
    Commented Jan 20, 2022 at 13:42

2 Answers 2

1
\$\begingroup\$

I don't see any issues with your ball movement. The shadow is bumpy.

Shadow maps are textures with pixels, but smoothing is applied so that low resolution shadows don't appear pixelated. However, the shadows in the shadow map can still only move one pixel at a time. When the resolution is low, the pixels are large and the shadow motion will appear jumpy. Here's what it might look like without smoothing:

Low resolution shadow without smoothing

You can fix this by increasing the shadow resolution.

\$\endgroup\$
1
  • \$\begingroup\$ i turned off shadows and still feel the ball is bouncing, although it could be the resolution, yet i don't see any way to turn this up, from where I'm looking, where is resolution settings located-? \$\endgroup\$
    – Spring
    Commented Jan 21, 2022 at 8:31
0
\$\begingroup\$

Fixed! The ball was rolling rather than gliding, and because it wasn't smooth enough, gave me the feeling it was bumpy.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .