vikram
vikram

Reputation: 84

Ball shaking upon increasing its speed in Unity

I'm a beginner. I'm making a ball game in Unity, in which ball have to avoid the collision with the obstacle. In the game, I'm increasing the ball speed in every 3 seconds. Everything's working fine, but in the middle of game, I noticed the ball starts shaking, the speed decreases, and the camera can't catch the ball. I attached physics material to all gameobjects, and made all frictions zero, but the ball is still shaking.

Here is the link of the my game and the problem. Have a look: https://youtu.be/TR4M5whweTk

Here is the script attached to the ball:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
public Text gameOverText;
public Text scoreText;
public bool isGameOver;
public float speeder;

public float Score;
Touch touch;
public float speedmodifier ;

public float speed = 5;
// Start is called before the first frame update

private void Awake()
    {
    isGameOver = false;
    Score = 0;
    if(PlayerPrefs.GetFloat("HighScore") == 0)
    {
        PlayerPrefs.SetFloat("HighScore", 0);
    }
    PlayerPrefs.SetFloat("Score", Score);
}
void Start()
{
    speeder = 4f;
    gameOverText.enabled = false;
    speedmodifier = 0.01f;
   // GetComponent<Rigidbody>().velocity = new Vector3(0,0,speed);

}

// Update is called once per frame
void Update()
{   if(speeder >= 0)
    {
        speeder -= Time.deltaTime;
    }
    if (speeder<= 0 && speed < 50)
    {
        speed++;
        speeder = 4f;
   
    }
    Debug.Log(speed); 
    if (isGameOver == false)
    {
        Score++;
    }
    

    scoreText.text = "Score : " + Score ;
    
    if (Input.touchCount >0 && transform.position.x >= -3.5f &&  transform.position.x <= 3.5f)
    {
        touch = Input.GetTouch(0);
        transform.Translate(touch.deltaPosition.x * speedmodifier,0,0);
    }
    else if(transform.position.x > 3.5f)
    {
        transform.position = new Vector3(3.49f,transform.position.y,transform.position.z);
    }
    else if(transform.position.x < -3.5f)
    {
       transform.position = new Vector3(-3.49f,transform.position.y,transform.position.z) ;
    }
    

     if (Input.GetKey(KeyCode.RightArrow) && transform.position.x < 3.5f)
    {
        transform.Translate(Vector3.right*speed* Time.deltaTime);
    }


     if (Input.GetKey(KeyCode.LeftArrow) && transform.position.x >-3.5f)
    {
        transform.Translate(Vector3.left*speed* Time.deltaTime);
    }

     
   transform.Translate(Vector3.forward * speed * Time.deltaTime);
}

private void OnTriggerEnter(Collider other)
{
    if(other.gameObject.tag == "enemy")
    {
        isGameOver = true;
        StartCoroutine("Wait");
        GetComponent<MeshRenderer>().enabled = false;
        gameObject.GetComponentInChildren<TrailRenderer>().enabled = false;
        gameOverText.enabled = true;

        if(PlayerPrefs.GetFloat("HighScore") < Score)
        {
            PlayerPrefs.SetFloat("HighScore", Score);
            PlayerPrefs.SetFloat("Score", Score);
        }
        else
        {
            PlayerPrefs.SetFloat("Score", Score);
        }

    }
}

IEnumerator Wait()
{
    Debug.Log(" My HighScore is : " + PlayerPrefs.GetFloat("HighScore"));
    Debug.Log(" Score is : " + PlayerPrefs.GetFloat("Score"));
    yield return new WaitForSeconds(3f);
    SceneManager.LoadScene(1);
}


}

Upvotes: 2

Views: 374

Answers (3)

Fattie
Fattie

Reputation: 12641

OK you SHOULD NOT be using unity phsyics for this.

It's a "raster" game (and that's fun).

  1. remove all physics everything. completely remove rigidbody, collider, etc

  2. simply move the ball and/or scenery using a calculation each time.

(It's basically just frame time * speed, obviously.)


(Note, you can use strictly triggers - if you want - simply to know if the "ball" is near a "stick". But you can do that with 1 line of code, really no need for colliders/etc.)

Upvotes: 2

IBXCODECAT
IBXCODECAT

Reputation: 134

Your Question

I just want to make sure that I am understanding your question correctly first, I believe you are asking for a way to prevent the ball from shaking during the game. (Let me know if this is incorrect, if so I apologize). From what I have gathered, this does not seem like a code based issue

Collision Detection

In unity, the rigidbody component has a field named 'Collision Detection' and this is set to discrete by default. This means that unity will look in direction of travel for a possible collider every now and again. That is an okay method to use when trying to save computer resources, however when in constant contact or at high velocities it is best to use the 'continuous' mode. This will check for a collision every physics update which occurs every physics time-step.

Colliders

Colliders in unity are a little finicky. Most notorious are mesh colliders as they have to be more heavenly processed/calculated. Using a box collider for your ground might give more accurate physics results.

Other Solutions

Whilst looking at your game video you published on YouTube, it looks like using physics may not be able to work. Since you are only traveling left, right, and forward globally, I would suggest implementing a movement script that uses an object's transform component instead of physics. This will be less resource intensive and will save further debugging headaches in the long run.

Upvotes: 1

Yeis Gallegos
Yeis Gallegos

Reputation: 484

As BugFinder pointed out you are actually not moving the ball with physics, but just teleporting the ball with Transform.Translate which might be affecting the shacking issue, the other possible problem is that your ball speed might be varying due to a collision with the road, have you tried making the road RigidBody to Kinematic? so it won't affect the speed of the ball

Upvotes: 2

Related Questions