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

Making object move from left to right?

Discussion in 'Scripting' started by Quist, Apr 11, 2015.

  1. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    Hey everybody i´m currently making a ball rolling game.

    I´m looking for how to make a platform move from the left to the right.
    So when it etc. hits the X position 10, it will move to X - 10 and then repeat.

    I currently have a script but the issue is that whenever it hits its max position to the right it makes a very hard transition to moving to the left so the player falls off.

    And whenever my player interacts with it its speed gets screwed so it moves slowly.

    My Script

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var platform : Transform;
    4. var startTransform : Transform;
    5. var endTransform : Transform;
    6. var platformSpeed : float;
    7.  
    8. var direction : Vector3;
    9. var destination : Transform;
    10.  
    11. function Start()
    12. {
    13.     SetDestination(startTransform);
    14. }
    15.  
    16.  
    17. function FixedUpdate ()
    18. {
    19.     platform.GetComponent.<Rigidbody>().MovePosition(platform.position + direction * platformSpeed * Time.fixedDeltaTime);
    20.    
    21.     if(Vector3.Distance(platform.position, destination.position) < platformSpeed * Time.fixedDeltaTime)
    22.     {
    23.         SetDestination(destination == startTransform ? endTransform : startTransform);
    24.     }
    25. }
    26.  
    27. function OnDrawGizmos()
    28. {
    29.     Gizmos.color = Color.green;
    30.     Gizmos.DrawWireCube(startTransform.position, platform.localScale);
    31.  
    32.     Gizmos.color = Color.red;
    33.     Gizmos.DrawWireCube(endTransform.position, platform.localScale);
    34. }
    35.  
    36. function SetDestination(dest : Transform)
    37. {
    38.     destination = dest;
    39.     direction = (destination.position - platform.position).normalized;
    40.    
    41. }
     
  2. Xavior87

    Xavior87

    Joined:
    Feb 22, 2015
    Posts:
    23
    You could just subtract or add onto the x value through a Vector3.

    public float subtractSpeed; //Move speed
    public float maxAmount; //The maxed amount
    public bool hitTheEnd; //Has the platform hit the max Value?
    public float maxLeftValue;
    public float maxRightValue;

    public FixedUpdate(){

    float moreSubtract;
    moreSubtract = subtractSpeed * Time.deltaTime;
    if(hitTheEnd == false){
    maxAmount -= moreSubtract;
    if(transform.position.x > maxLeftValue){
    transform.position = new Vector3(maxAmount,0,0);
    }
    else{
    hitTheEnd = true;
    }
    }
    else{
    maxAmount += moreSubtract;
    if(transform.position.x < maxRightValue){
    transform.position = new Vector3(maxAmount,0,0);
    }
    else{
    hitTheEnd = false;
    }
    }


    Let me know if this works! I haven't tested it!
     
  3. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    Hey i´ve put it into unity now, but there is a parsin error somewhere but i cant find it, can you help me?

    Btw i dont use Csharp but i use JS, but ill try it anyway

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public float : subtractSpeed;
    5. public float : maxAmount;
    6. public bool : hitTheEnd;
    7. public float : maxLeftValue;
    8. public float : maxRightValue;
    9.  
    10. public FixedUpdate()
    11. {
    12.    
    13.     float moreSubtract;
    14.  
    15.     moreSubtract = subtractSpeed * Time.deltaTime;
    16.     if(hitTheEnd == false)
    17.     {
    18.         maxAmount -= moreSubtract;
    19.         if(transform.position.x > maxLeftValue)
    20.         {
    21.             transform.position = new Vector3(maxAmount,0,0);
    22.         }
    23.         else
    24.         {
    25.             hitTheEnd = true;
    26.         }
    27.     }
    28.     else
    29.     {
    30.         maxAmount += moreSubtract;
    31.         if(transform.position.x < maxRightValue)
    32.         {
    33.             transform.position = new Vector3(maxAmount,0,0);
    34.         }
    35.         else
    36.         {
    37.             hitTheEnd = false;
    38.         }
    39.     }
    40. }
     
  4. IchBinJager

    IchBinJager

    Joined:
    Dec 23, 2014
    Posts:
    127
    What parser error does it give you?
     
  5. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    i tried something else, i have now tried using the animation option and a simple script which looks like this.
    Code (JavaScript):
    1. function GoRight()
    2. {
    3.         GetComponent.<Animation>().CrossFade("Right");
    4.  
    5.  
    6. }
    7. function GoLeft()
    8. {
    9.         GetComponent.<Animation>().CrossFade("Left");
    10.  
    11. }
    But the problem is that even tho there´s CrossFade the transition is still so harsh that it knocks the player off.

    I call the GoRight and GoLeft functions inside the animation by an event.

    Can you help me with why it doesnt make a smooth transition please :3