Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I need help with a script for a sphere moving between 2 walls 20 times

Discussion in 'Scripting' started by Geisi1995, Jul 25, 2017.

  1. Geisi1995

    Geisi1995

    Joined:
    Jul 25, 2017
    Posts:
    17
    This is my script so far
    #pragma strict
    var right : Transform;
    var left : Transform;
    var count : int = 0;

    function Start () {
    transform.position = right.position;



    }

    function Update () {


    }

    function OnCollisionEnter ( collision : Collision) {
    if( collision.gameObject.name == "Right")
    transform.position = Vector3.Lerp(transform.position, left.position, Time.time );
    if( collision.gameObject.name == "Left")
    transform.position = Vector3.Lerp(transform.position, right.position, Time.time );
    }
    -------------------------------
    So the problem is, the sphere starts at the "right" wall and moves smooth 2 the left wall, but then the sphere starts 2 flip around instead of going back smoothly to the "right" wall.
    I would appreciate some help right there
     
    Last edited: Jul 25, 2017
  2. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    function OnCollisionEnter ( collision : Collision) {
    if( collision.gameObject.name == "Right")
    transform.position = Vector3.Lerp(transform.position, left.position, Time.time );
    else if( collision.gameObject.name == "Left")
    transform.position = Vector3.Lerp(transform.position, right.position, Time.time );
    }

    Try that please
     
  3. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    Also check that Left and RIght Transform are the correct walls so that Right isn't the left wall, that would explain it trying to lerp into itself
     
  4. Geisi1995

    Geisi1995

    Joined:
    Jul 25, 2017
    Posts:
    17
    ok give me a second.
     
  5. Geisi1995

    Geisi1995

    Joined:
    Jul 25, 2017
    Posts:
    17
    still the same problem. like the sphere should run between those walls smoothly, the first run from left to the right is smooth, but then ist just bouncing and not going smoothly ;/
     
  6. Geisi1995

    Geisi1995

    Joined:
    Jul 25, 2017
    Posts:
    17
    or maybe is there any other option i can use instead of vetor3.lerp? i already tried to work with the rigidbody and add forces forwards, but its not working as well. it sounds so simple but i have no clue
     
  7. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    Can you please use a fixed float instead of Time.time? just say 10f
     
    Last edited: Jul 25, 2017
  8. Geisi1995

    Geisi1995

    Joined:
    Jul 25, 2017
    Posts:
    17
    tried it, but its still not working. do you have an idea how to make an object move from point A to B without vector3.lerp or using the transform.translate stuff?
     
  9. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    Well the thing is you need to put the Lerp in the update function or a coroutine
     
  10. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    #pragma strict
    public var right : Transform;
    public var left : Transform;
    var count : int = 0;
    var goLeft : boolean = false;
    var speed : int = 4;
    function Update(){
    if(count <= 20){
    if(goLeft){
    transform.position = Vector3.Lerp(transform.position,left.position, Time.deltaTime*speed);
    }
    else if(!goLeft){
    transform.position = Vector3.Lerp(transform.position,right.position, Time.deltaTime*speed);
    }
    }
    }
    function OnCollisionEnter(collision:Collision){
    if(collision.gameObject.name == "Right"){
    goLeft = true;
    count++;
    }
    else if(collision.gameObject.name == "Left"){
    goLeft = false;
    count++;
    }
    }
     
    Last edited: Jul 25, 2017
    Geisi1995 likes this.
  11. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    That should work, you might have to multiply the Time.deltaTime with a float to make it go a bit faster though :D
    Vector3.MoveTowards is similar to Vector3.Lerp aswell takes the same arguments/parameters
     
  12. Geisi1995

    Geisi1995

    Joined:
    Jul 25, 2017
    Posts:
    17
    #pragma strict
    var right : Transform;
    var left : Transform;
    var count : int = 0;
    var speed : int = 4;
    private var goLeft : boolean;

    function Start () {
    transform.position = right.position;
    }

    function Update(){
    if(goLeft== true){
    Vector3.Lerp(transform.position,left.position, Time.deltaTime);
    }
    else{
    Vector3.Lerp(transform.position,right.position, Time.deltaTime);
    }
    }
    function OnCollisionEnter(collision:Collision){
    if(collision.gameObject.name == "Right"){
    goLeft = true;
    }
    else if(collision.gameObject.name == "Left"){
    goLeft = false;
    }
    }

    -------
    did i used it right? well now its moving from right to left but then it stops at the left wall and not going back to the right.
     
  13. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    I updated my answer and you can remove the Start function
    You can just copy my answer above as the entire function
     
  14. Geisi1995

    Geisi1995

    Joined:
    Jul 25, 2017
    Posts:
    17
    thank you a lot ! it works, god bless you :)!
     
  15. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    Sorry, just tested it, there were some syntax errors, I prefer C# :D I suggest you start learning it aswell they are removing Unityscript in 2017.3

    Glad you got it working :)

    Also I suggest MoveTowards if you don't need the easing in/out from Lerp
     
  16. Geisi1995

    Geisi1995

    Joined:
    Jul 25, 2017
    Posts:
    17
    im working with unity since 7 months now, but i only worked with javascript, but yea i will check out C# soon. have a great day