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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

objects moving on there on :(

Discussion in 'Scripting' started by sarthakz9, Jun 3, 2015.

  1. sarthakz9

    sarthakz9

    Joined:
    Jun 3, 2015
    Posts:
    19
    i created a simple motion script that takes 4 inputs.... up,down,left and right..
    script is working right when there is only one rigid body(the sphere which i`m contrlling)
    but when i create a second sphere, having a rigid body component but not the controlller script...
    problem occurs..

    they will move right for few inputs and after that .... they will start to move on there own ....
    sometimes oppsite from each other

    however the variables x and y i used in my script are still showing 0 in inspector tab

    my script :

    using UnityEngine;
    using System.Collections;

    public class NewBehaviourScript : MonoBehaviour {

    public float x,y ;
    // Use this for initialization
    void Start () {
    Debug.Log(" i am alive !");
    }

    void OnGUI ()
    {
    if (Input.GetKey ("up"))
    {
    y=0.05f; ;
    }
    else if (Input.GetKey ("down"))
    {y=-0.05f;
    }
    else if (Input.GetKey ("left"))
    {x=-0.05f;
    }
    else if (Input.GetKey ("right"))
    {x=0.05f;
    }

    if(GUI.RepeatButton (new Rect(20,40,60,20), "-->"))
    {
    x=0.05f;
    }

    if(GUI.RepeatButton (new Rect(80,40,60,20), "<--"))
    {
    x=-0.05f;
    }

    if(GUI.RepeatButton (new Rect(40,20,80,20), "^"))
    {
    y=0.05f;
    }

    if(GUI.RepeatButton (new Rect(40,60,80,20), "V"))
    {
    y=-0.05f;
    }
    }

    // Update is called once per frame
    void Update ()
    {
    transform.position += Vector3.right * x;
    transform.position += Vector3.forward * y;
    y=x=0;
    }
    }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're probably getting some sort of velocity/momentum build up from the physics engine "doing it's thing"... try setting the rigidbodies to kinematic
     
    sarthakz9 likes this.
  3. sarthakz9

    sarthakz9

    Joined:
    Jun 3, 2015
    Posts:
    19
    yooo!! ..... correct ;)