Search code examples
unity-game-enginegame-physics

I want to create the ball movement like the one in 'chilly snow' game. I want to curve the ball, but it revolves


I am new to unity and don't know a lot of stuff. I've been watching tutorials and I saw one in which the guy created a replica of famous 'Chilly Snow'. The game is complete but the movement of ball isn't like the one in chilly snow. The ball starts orbiting continuously when I press mouse button. I wanted to know how to create that kind of movement, so that the ball turns left and right in a curve but doesn't go in to an orbit. I googled a lot but wasn't able to find my required result. I would really appreciate if anyone could point me in the right direction. Images are attached.Chilly Snow | Movement of my ball

public class movement : MonoBehaviour {

private float points;
public float playerSpeed;
private float rotationSpeed;
public Text score;
private bool isMovingLeft;
public GameObject player;
public bool isDead;




void Start () {

    Time.timeScale = 0;
    isDead = false;
    isMovingLeft = true;
    points = 0;



}


void Update () 
{
    if (isDead == false) 
    {
        points += Time.deltaTime;
    }

    transform.Translate (Vector3.down * playerSpeed * Time.deltaTime);

    if (Input.GetMouseButtonDown (0)) 
    {
        Time.timeScale = 1;
        isMovingLeft = !isMovingLeft;
        rotationSpeed += 0.5f * Time.deltaTime;

    }

    if (Input.GetMouseButton (0)) 
    {
        rotationSpeed = 1f;
    }

    if (isMovingLeft) {
        rotationSpeed += 1.5f * Time.deltaTime;
        transform.Rotate(0,0,rotationSpeed);

    } else
        transform.Rotate(0,0, - rotationSpeed);
}


void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Obstacle") {
        Die ();
    }
}

public void Die()
{


    playerSpeed = 0f;
    isDead = true;
    Invoke ("Restart", 2f);

}

void Restart(){

    SceneManager.LoadScene ("Ski_scene_1");

}

void FixedUpdate()
{
    score.GetComponent<Text>().text = points.ToString("0");
}

}


Solution

  • Here is how I would approach it without doing a rotation... using your code.

    public class movement : MonoBehaviour {
    
    private float points;
    public Text score;
    public GameObject player;
    public bool isDead;
    
    private float currentXSpeed;
    private float targetSpeed;
    public float maxXSpeed;
    public float speedChange;
    
    void Start () {
        Time.timeScale = 0;
        isDead = false;
        isMovingLeft = true;
        points = 0;
        targetSpeed = maxXSpeed;
    }
    
    
    void Update () 
    {
        if (isDead == false) 
        {
            points += Time.deltaTime;
        }
    
        if(Input.GetMouseButtonDown(0))
        {
             Time.timeScale = 1;
             targetSpeed = -targetSpeed;
        }
    
        currentSpeed = mathf.MoveTowards(currentSpeed, targetSpeed, speedChange * Time.deltaTime);
        Vector3 movementDirection = new Vector3(currentSpeed, Vector3.down.y * playerSpeed, 0.0f);
        transform.Translate (movementDirection * Time.deltaTime);
    }
    
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Obstacle") {
            Die ();
        }
    }
    
    public void Die()
    {
        playerSpeed = 0f;
        isDead = true;
        Invoke ("Restart", 2f);
    }
    
    void Restart(){
        SceneManager.LoadScene ("Ski_scene_1");
    }
    
    void FixedUpdate()
    {
        score.GetComponent<Text>().text = points.ToString("0");
    }
    
    }