Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity.Cant make my sphere move

Discussion in 'Getting Started' started by gab123, Mar 20, 2016.

  1. gab123

    gab123

    Joined:
    Mar 6, 2016
    Posts:
    14
    Hi,
    For the first I wanna say Im a begginner and Im sorry if my mistake was very simple.
    But Im making a game which should be simillar to ZigZag game for android.
    But when I cant make my ball move I tried so many different ways but nothing worked. This is some code :

    using UnityEngine;

    using System.Collections;

    publicclasssphere : MonoBehaviour

    {



    void Update()

    {

    float time = 1;

    int direction = 1;

    Rigidbody rigidbody = GetComponent<Rigidbody>();

    time += (float)0.1;



    switch (direction)

    {

    case 1:

    // rigidbody.AddForce(-transform.forward * time);

    rigidbody.velocity = transform.TransformDirection(newVector3(time,0, 0));

    break;

    case 2:

    //rigidbody.AddForce(transform.forward * time);

    rigidbody.velocity = transform.TransformDirection(newVector3(time,0, 0));

    break;

    default:

    Debug.Log(direction);

    break;

    }

    if (Input.GetKey(KeyCode.LeftArrow))

    {


    direction = 1;


    }

    if (Input.GetKey(KeyCode.RightArrow))

    {



    direction = 2;



    }

    //rigidbody.AddForce(transform.right*time);

    rigidbody.velocity = transform.TransformDirection(newVector3(0, 0, time));

    }

    }

    The comment stuff are things i tried before and didnt work same. The thing which worked was simply changing position of the ball but it didnt look good.
    Please help.
    Thank you.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    I see you have a Rigidbody on your sphere. Take that off, and then move it by simply changing transform.position.
     
  3. gab123

    gab123

    Joined:
    Mar 6, 2016
    Posts:
    14
    Well, I need Righidbody because I want my ball to have gravity and I also don't want just to change position because thanthe screen jems
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Ah, that's unfortunate. Then you'll have to think much harder (and more clearly) about how your script should interact with the motion produced by the physics engine.
     
  5. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    Hey, have you tried the Roll-A-Ball tutorial? It's an intro to Unity where you build a short game that rolls a ball.
     
    JoeStrout likes this.
  6. gab123

    gab123

    Joined:
    Mar 6, 2016
    Posts:
    14
    Iron-Warrior Not yet, but Ill have a look on it. Thanks, for help.