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

Is there any function to move object by localPosition?

Discussion in 'Scripting' started by qrwe1423, Aug 30, 2016.

  1. qrwe1423

    qrwe1423

    Joined:
    Aug 30, 2016
    Posts:
    3
    Hello,

    I build a small project to describe my question.



    Here is my code.
    Code (CSharp):
    1.     public GameObject player;
    2.     public GameObject weapon;
    3.  
    4.     void Start()
    5.     {
    6.  
    7.         player.transform.RotateAround(player.transform.position, new Vector3(0, 1, 0), 45);
    8.  
    9.         //#1
    10.         //weapon.transform.localPosition += new Vector3(2, 0, 0);
    11.        
    12.         //#2
    13.         //weapon.transform.position = Vector3.Slerp(weapon.transform.position, player.transform.position + new Vector3(2, 0, 0), 999);
    14.  
    15.     }
    And when I run it, the "player" is rotated 45°


    When I uncommented #1, the "weapon" got to the right position.


    A smooth movement may be better, but when I commented #1 and uncommented #2, I got this.


    I think I know what happened here... and I wonder is there any function to move object by localPosition?
    Any help will be appreciated:)
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    In #2, why are you using the global position values? Try using the local values instead, like in #1.
     
  3. qrwe1423

    qrwe1423

    Joined:
    Aug 30, 2016
    Posts:
    3
    Oops...such a simple mistake...
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    The problem your running into, is the weapon is a child of the player(cube). so you *ARE* moving it by "localPosition". That is, its transform position is relative to the Cube. So when you were adding + Vector3(2,0,0) you just adjusting it position relative to the weapon.
    But your Slerp was between (weapon's Local Position Relative to the Cube, Cube's absolute Position in the world+2, time Factor). So the only reason the cylinder was even close to the cube was your cube was probably near the origin. If your cube had been at world position +100,0,+100. Then when you set the cylinder's local position to 102,0,100 it would have been that many units AWAY from the cube. and probably off your screen.

    Your Slerp should work but change it to this:
    Code (CSharp):
    1. weapon.transform.position = Vector3.Slerp(weapon.transform.position, weapon.transform.position + new Vector3(2, 0, 0), 999);
    Note: the timeFactor (999) is clamped in the Slerp code to the range 0->1. So your 999 is the same as typing 1.0 which will always return the full second position (weapon.transform.position)+new Vector3(2, 0, 0),.
    If you want to move the cylinder slowly away from the cube try this:
    Code (CSharp):
    1.     void Start () {
    2.         StartCoroutine(MoveCylinder());
    3.     }
    4.  
    5.     IEnumerator MoveCylinder()
    6.     {
    7.         float currentTime = 0.0f;
    8.         float normalizedDelta;
    9.         Vector3 originalPosition = weapon.transform.position;
    10.         Vector3 destinationPosition = weapon.transform.position + new Vector3(5.0f, 0.0f, 0.0f);
    11.         while (currentTime < 3.0f)
    12.         {
    13.             currentTime += Time.deltaTime;
    14.             // since we are taking 3s to move
    15.             // We need to normalize that time in the range of 0->1
    16.             normalizedDelta = currentTime / 3.0f;
    17.             weapon.transform.position = Vector3.Lerp(originalPosition, destinationPosition, normalizedDelta);
    18.             yield return null;
    19.         }
    20.     }
    Also note I used LERP instead of SLERP. If your just moving in a straight line, I'd use LERP on the position vector as its Linear interpretion of it. SLERP takes both magnitude and the angle into account, so it will end up in the same place as LERP, but it can sometimes do a arc type of movement.
     
    deniztkaymakci and qrwe1423 like this.
  5. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    OMG the is the exact function I've been looking for, forever! I wanted my player to be able to walk up to a box an Climb it by hitting F. This is LIT!