Search Unity

Can't manage to translate or MoveTowards with animation...

Discussion in 'Animation' started by adralith, Mar 21, 2017.

  1. adralith

    adralith

    Joined:
    Mar 14, 2017
    Posts:
    3
    Hi,
    I am kinda new to unity and I'm working on a little 3D game in which the character has to move thanks to "keywords" in a text box such as "move" "turnleft" etc... (Textboxes are replaced by buttons temporarily)



    I need the character to move from a cube to another only, if I click the button on the left, it has to go to the blue cube in front of him.
    I've got this code from now :

    Code (CSharp):
    1. public void goForward()
    2.         {
    3.             transform.Translate(0.0f, 0.0f, 1.0f );
    4.            
    5.             m_Character.UpdateAnimator(new Vector3(0.0f, 0.0f, 1.0f * Time.deltaTime ));
    6.             m_Character.m_Animator.SetFloat("Forward", 1, 0.1f, Time.deltaTime );
    7.         }
    So yes, it's going forward but toooo quickly...
    I've tried using Time.deltaTime in the transform.translate(); but then when I press the button, the characters goes forward for like 1/100 of the wanted distance...
    Have you got any solution to either slow down the tranform.translate and make the animation "seeable" or to use MoveTowards ? ( I used it aswell but it teleported too)...

    by the way : I'm using Ethan, an included character in Unity


    Thanks !
     
  2. Maxpeinas

    Maxpeinas

    Joined:
    Feb 19, 2014
    Posts:
    16
    You can add this in coroutine

    Code (CSharp):
    1.  
    2.  
    3. void Start()
    4. {
    5.      StartCoroutine(GoForward());
    6. }
    7.  
    8. public IEnumerator GoForward()
    9. {
    10. Vector3 targetPosition = target.transform.position;
    11.      while(Vector3.Distance(targetPosition, attacker.transform.position) > wantedDistance)
    12.      {
    13.           attacker.transform.position = Vector3.MoveTowards(attacker.transform.position, targetPosition, speed * Time.deltaTime);
    14.           yield return null;
    15.      }
    16. }
    or in update

    Code (CSharp):
    1.  
    2. if(Vector3.Distance(targetPosition, attacker.transform.position) > wantedDistance)
    3.      attacker.transform.position = Vector3.MoveTowards(attacker.transform.position, targetPosition, speed * Time.deltaTime);
    Also take note that transform.Translate and Vector3.MoveTowards must be used in Update() or Coroutine i think, otherwise it will move only one frame before it stops.
     
  3. adralith

    adralith

    Joined:
    Mar 14, 2017
    Posts:
    3
    Hey,
    Thanks for your answer !
    It works fine with animation now, there is still a remaining problem, the character won't stop walking when it is facing "x axis" (I mean Rotation 0,90,0). Have you got any tips ?
    Here are the things I made (Thanks to you) :
    (The currentPos var is updated in update() )
    Code (CSharp):
    1.  public IEnumerator GoForward()
    2.         {
    3.             var speed = 1.0f;
    4.             Vector3 futurePos = new Vector3(1.0f,0,0); //the var that contains the position for the character to go
    5.          
    6.            //if looking at the right (+x axis)
    7.             if (transform.rotation == Quaternion.Euler(0, 90, 0))//I think this is where it doesn't work
    8.             {
    9.               futurePos = new Vector3(currentPos.x+1.0f, currentPos.y, currentPos.z);
    10.             }
    11.             //if looking at us (-z axis)
    12.             else if (transform.rotation == Quaternion.Euler(0, -180, 0) || transform.rotation == Quaternion.Euler(0, 180, 0))
    13.             {
    14.                 futurePos = new Vector3(currentPos.x, currentPos.y, currentPos.z - 1.0f);
    15.             }
    16. //if looking at the left (-x axis)
    17.             else if (transform.rotation == Quaternion.Euler(0, -90, 0))//here too aswell...
    18.             {
    19.                 futurePos = new Vector3(currentPos.x - 1.0f, currentPos.y, currentPos.z);
    20.  
    21.             }
    22. //if it's turning back to us(+z axis)
    23.             else if (transform.rotation == Quaternion.Euler(0, 0, 0))
    24.             {
    25.                 futurePos = new Vector3(currentPos.x, currentPos.y, currentPos.z + 1.0f);
    26.  
    27.             }
    28.  
    29.             while (Vector3.Distance(futurePos, m_Character.transform.position) > .1f)
    30.             {    
    31.                 m_Character.transform.position = Vector3.MoveTowards(m_Character.transform.position, futurePos, speed * Time.deltaTime);
    32.                 m_Character.m_Animator.SetFloat("Forward", 1, 0.0f, Time.deltaTime);
    33.                 yield return null;
    34.             }
    35.         }
    Here is the function launched on click, as you advised me :


    Code (CSharp):
    1.  public void lancerMarche()
    2.         {
    3.             StartCoroutine(GoForward());
    4.         }
     
  4. Maxpeinas

    Maxpeinas

    Joined:
    Feb 19, 2014
    Posts:
    16
    How do you set rotation ? Do you use transform.rotation = Quaternion; or slerp etc ? Try using <= and >= instead of ==. Also it may be easier to compare single axis instead of quaternions, like transform.rotation.x == x and so on.
     
  5. adralith

    adralith

    Joined:
    Mar 14, 2017
    Posts:
    3
    I'm setting rotation this way (might not be the good way tho) :
    Code (CSharp):
    1. public void turnRight()
    2.         {
    3.             transform.Rotate(0, 90.0f, 0);
    4.         }
    edit : Changed the way I made it rotate, works fine now ! Thanks a lot !
     
    Last edited: Mar 23, 2017