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

transform.positon problem

Discussion in 'Scripting' started by verderveremem, Jul 21, 2014.

  1. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    Hi folks,

    i am changing the position of the my gameobject by reacging it's transform component.I want to take a soft passing but its passing very fast(like beamed up).
    So is there any idea to make it soft? Here is the code, thanks.
    Code (CSharp):
    1. void Update () {
    2.         if (Input.GetTouch(0).phase==TouchPhase.Began)
    3.         {
    4.          
    5.             if (i == 1)
    6.             {
    7.              
    8.                GMaincar.transform.position = new Vector3(-0.72f, GMainpatates.transform.position.y, 0);
    9.                 i--;
    10.              }
    11.  
    12.            else if (i == 0)
    13.             {
    14.                 GMaincar.transform.position = new Vector3(0.72f, GMainpatates.transform.position.y, 0);
    15.                 i++;
    16.             }
    17.  
    18.         }
     
    Last edited: Jul 21, 2014
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Look at Vector3.MoveTowards.
     
    verderveremem likes this.
  3. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    hi,
    i tried to this code block but its still isnt working :( can u help me guys

    Code (CSharp):
    1. void Update () {
    2.         if (Input.GetMouseButton(0))//Input.GetTouch(0).phase==TouchPhase.Began)
    3.         {
    4.            
    5.             if (i == 1)
    6.             {
    7.  
    8.                 GMaincar.transform.position = Vector3.MoveTowards(GMaincar.transform.position, new Vector3(-0.72f, GMaincar.transform.position.y, 0), speed * Time.deltaTime);//(-0.72f, GMaincar.transform.position.y, 0);
    9.                 i--;
    10.              }
    11.  
    12.            else if (i == 0)
    13.             {
    14.                 GMaincar.transform.position = Vector3.MoveTowards(GMaincar.transform.position, new Vector3(0.72f, GMaincar.transform.position.y, 0), speed * Time.deltaTime);
    15.                 i++;
    16.             }
    17.  
    18.         }
    19.    
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Ofcourse, that does not work. You are changing the value of i between 0 and 1 every frame. What are you using i for?
     
    verderveremem likes this.
  5. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    i am using it for change to positon when touched to the screen :) how can i solve this problem?
     
  6. Coletrain91

    Coletrain91

    Joined:
    Jan 29, 2014
    Posts:
    12
    So, are you simply trying to toggle between two different positions on the screen, but instead of having it instantly jump back and forth, you want it to smoothly go from one point to the other over, say, a full second?
     
    verderveremem likes this.
  7. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    You need to determine where the touch is on the screen and multiply the direction of the player with the normal of the axis you want to move in. If the touch is at Vector2(-2,3), multiply by -1.
     
  8. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    yes i want this :( but i still dont solve the problem
     
  9. Coletrain91

    Coletrain91

    Joined:
    Jan 29, 2014
    Posts:
    12
    So, I'm still not entirely sure on the mechanics of how you want things to work, but this should get you a bit closer to your goal. I've tried to make it as readable as possible and comment everything I did, but if you have any questions, let me know. I'll try to explain it in more detail.

    You are going to also need a few control variables, which I declare right above the Update() function, but you will probably want to move them near any other variables that you declare in this script.

    Code (CSharp):
    1. bool targetingLeft = false; //This is your 'i variable' replacement since I can't see where it is coming from
    2.     Vector3 target; //GMaincar will need a target position to move towards later on
    3.     Vector3 startPosition; //This is the position of GMaincar when a target is set (so we have a constant movement)
    4.     float delta = 0.0f; //This is the percent of the move that is done. When it hits 1, we will have reached the target
    5.     bool hasTarget = false;
    6.  
    7.     void Update ()
    8.     {
    9.         if (Input.GetMouseButtonDown(0))//Input.GetTouch(0).phase==TouchPhase.Began)
    10.         {          
    11.             //Since the screen was touched, or the mouse clicked in this case, we need to assign a target
    12.  
    13.             //So that you can move back and forth anytime, let's flip the target each time the screen is touched
    14.             targetingLeft = !targetingLeft; //Makes 'targetingLeft' the opposite, thus flipping it
    15.  
    16.             //If we are targeting the left side (the -0.72 value) set that as the target
    17.             if(targetingLeft == true)
    18.                 target = new Vector3(-0.72f, GMaincar.transform.position.y, 0);
    19.             else //If 'targetingLeft' is false, we will assign the right side as the target
    20.                 target = new Vector3(0.72f, GMaincar.transform.position.y, 0);
    21.  
    22.             startPosition = GMaincar.transform.position;
    23.  
    24.             hasTarget = true; //Set the 'hasTarget' flag so we know that GMaincar should move later on
    25.             delta = 0.0f; //Also reset delta if it has changed
    26.          
    27.             //Now that we have a target, we can move towards that target in the standard update cycle                      
    28.         }
    29.      
    30.         //Outside of the Input section
    31.      
    32.         if(hasTarget)
    33.         {
    34.             //Add the Time.deltaTime to the delta variable to update the 'percent of move that is complete'
    35.             delta = delta + Time.deltaTime;
    36.  
    37.             //Move GMaincar towards the target
    38.             GMaincar.transform.position = Vector3.Lerp(startPosition, target, delta);
    39.  
    40.             if(delta >= 1.0f)
    41.             {              
    42.                 //No longer needs to move, so set hasTarget to false
    43.                 hasTarget = false;
    44.             }
    45.         }
    46.     }
     
    verderveremem likes this.
  10. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    OMG THATS WORK. THANK YOU SO MUCH....THANK YOU!! :))
     
    Coletrain91 likes this.