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

Movement with AddForce and than slowing down until the velocity is 0

Discussion in 'Scripting' started by Like-A-Sir, Nov 26, 2015.

  1. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33
    Good day!

    I am working a script for an flying enemy in an 2d platformer . It will fly from left to right in a specific distance.I tryed using velocity,but I can't figure out how to decrease that speed until it reach the destination(For exemple it will have the velocity 6 and when it reach the endPoint the veocity will be 2).After reaching the destination it should return to the initial position and that is the behaviour of the enemy.

    I tryed using Vector3.Lerp,but I don't know how to decrease the speed.Please tell me a function or sth like this.

    Thank you very much!
     
  2. Diericx

    Diericx

    Joined:
    Jun 8, 2013
    Posts:
    62
    Can you share the code you are using?

    For the enemy, you're saying you just want it to move back and forth right? I think Lerp would be better than trying to do it with physics. Could you also share the code you used when trying to yse Vector3.Lerp?
     
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    you can not set the velocity of your rigidbody, you can apply force in the opposite direction.
    rigidBody.AddRelativeForce(-rigidBody.velocity*force, ForceMode.VelocityChange); //something like this...
     
  4. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33

    Of course!Here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FlyingEnemyMove : MonoBehaviour {
    5.  
    6.     public float thrust;
    7.     private Rigidbody2D rb;
    8.     public float stoppingSpeed;
    9.     public float maxSpeed;
    10.     public float minSpeed;
    11.     public float decreasingAmountOfSpeed;
    12.     private int isIncreasing;
    13.     private int isDecreasing;
    14.  
    15.     // Use this for initialization
    16.     void Start ()
    17.     {
    18.         rb = GetComponent <Rigidbody2D> ();
    19.         isIncreasing = 1;
    20.         isDecreasing = 0;
    21.     }
    22.  
    23.  
    24.     void FixedUpdate()
    25.     {
    26.               StartCoroutine(physicsMoving ());
    27.     }
    28.  
    29.     void Update ()
    30.     {
    31.     }
    32.  
    33.     IEnumerator physicsMoving()
    34.     {
    35.         if (rb.velocity.x < maxSpeed && isIncreasing == 1)
    36.         {
    37.             rb.AddForce (new Vector2 (thrust, 0), ForceMode2D.Impulse);
    38.             yield return new WaitForSeconds(2f);
    39.         }
    40.         if (rb.velocity.x >= maxSpeed)
    41.             isIncreasing = 0;
    42.         if (rb.velocity.x >= minSpeed && isDecreasing == 0)
    43.         {
    44.             rb.AddForce (new Vector2 (decreasingAmountOfSpeed, 0),ForceMode2D.Impulse);
    45.             yield return new WaitForSeconds(2f);
    46.         }
    47.         if (rb.velocity.x == minSpeed)
    48.             isDecreasing = 1;
    49.         Debug.Log (rb.velocity.x);
    50.         //Debug.Log (isIncreasing);
    51.     }
    52.  
    53.     public void PullTrigger(Collider2D other)
    54.         {
    55.             if (other.tag == "Player")
    56.                 {
    57.                     //GetComponent<Rigidbody2D>().velocity =  Vector2.zero;
    58.                     //GetComponent<Rigidbody2D>().angularVelocity =  0f;  
    59.                 }
    60.         }
    61.    
    62. }
    63.  

    Unfortunately it is not decreasing smooth.I can't figure out how use an easy way to do this.Thank you!
     
  5. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33

    Thank you!

    Here is my code.Unfortunately the speed is decreasing too suddenly.I want a slowly decreasing of speed.I will be very happy and grateful if you will take a look on my code.Thank you

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FlyingEnemyMove : MonoBehaviour {
    5.  
    6.     public float thrust;
    7.     private Rigidbody2D rb;
    8.     public float distanceToStop;
    9.     public float stoppingSpeed;
    10.     private int isMovingLeft;
    11.     private Vector2 newPosition;
    12.     public float flyingDistance;
    13.     public float maxSpeed;
    14.     public float minSpeed;
    15.     public float decreasingAmountOfSpeed;
    16.     private int isIncreasing;
    17.     private int isDecreasing;
    18.  
    19.     // Use this for initialization
    20.     void Start ()
    21.     {
    22.         rb = GetComponent <Rigidbody2D> ();
    23.         isIncreasing = 1;
    24.         isDecreasing = 0;
    25.     }
    26.  
    27.  
    28.     void FixedUpdate()
    29.     {
    30.               StartCoroutine(physicsMoving ());
    31.     }
    32.  
    33.     void Update ()
    34.     {
    35.     }
    36.    
    37.  
    38.     IEnumerator physicsMoving()
    39.     {
    40.         if (rb.velocity.x < maxSpeed && isIncreasing == 1)
    41.         {
    42.             rb.AddForce (new Vector2 (thrust, 0), ForceMode2D.Impulse);
    43.             yield return new WaitForSeconds(2f);
    44.         }
    45.         if (rb.velocity.x >= maxSpeed)
    46.             isIncreasing = 0;
    47.         if (rb.velocity.x >= minSpeed && isDecreasing == 0)
    48.         {
    49.             rb.AddForce (new Vector2 (decreasingAmountOfSpeed, 0),ForceMode2D.Impulse);
    50.             yield return new WaitForSeconds(2f);
    51.         }
    52.         if (rb.velocity.x == minSpeed)
    53.             isDecreasing = 1;
    54.         Debug.Log (rb.velocity.x);
    55.         //Debug.Log (isIncreasing);
    56.     }
    57.  
    58.     public void PullTrigger(Collider2D other)
    59.         {
    60.             if (other.tag == "Player")
    61.                 {
    62.                     //GetComponent<Rigidbody2D>().velocity =  Vector2.zero;
    63.                     //GetComponent<Rigidbody2D>().angularVelocity =  0f;  
    64.                 }
    65.         }
    66.    
    67. }
    68.  
     
  6. Diericx

    Diericx

    Joined:
    Jun 8, 2013
    Posts:
    62
    I think you can set a rigidbody's velocity using Rigidbody.velocity
    http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

    @Like A Sir I feel like if you are just trying to animate an object it would be easier/more effective to lerp the position or create some sort of equation to translate it to the position. What was happening when you tried to use Vector3.Lerp?

    I might also be completely wrong about what you want, so for the code you showed you are saying it slows down too quickly...What happens when you change the variable decreasingAmountOfSpeed in Unity?
     
  7. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    try this
    Code (CSharp):
    1.  
    2. IEnumerator physicsMoving()
    3. {
    4.     Rigidbody2D rb = gameObject.GetComponent<Rigidbody2D>();
    5.     while(rb.velocity.x < maxSpeed)
    6.     {
    7.         rb.AddForce (newVector3 (thrust, 0), ForceMode2D.Impulse);  
    8.         Debug.Log ("increase "+rb.velocity.x);
    9.         yield return null;
    10.     }
    11.     while(rb.velocity.x > minSpeed)
    12.     {
    13.         rb.AddForce (newVector2 (decreasingAmountOfSpeed, 0), ForceMode2D.Impulse);
    14.         Debug.Log ("Decrease "+rb.velocity.x);
    15.         yield return null;
    16.     }
    17. }
    18.  
    seems you are right Diericx, thought it was read only.
     
    Last edited: Nov 28, 2015
  8. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33

    Well,I made a Lerp function which works perfect,but I don't know how to make the effect of brake if you know what I mean.The gameObject goes from pointA to pointB but with a constant speed,and I want for exemple to reduce the speed when I am at the half.Here is the Lerp code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LerpMoving : MonoBehaviour {
    5.  
    6.     float lerpTime = 1f;
    7.     float currentLerpTime;
    8.  
    9.     public float distanceToGo;
    10.  
    11.     float moveDistance = 10f;
    12.  
    13.     private Vector3 startPos;
    14.     private Vector3 endPos;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         startPos = transform.position;//new Vector3(transform.positon.x,transform.position.y,0);
    19.         endPos = new Vector3 (transform.position.x + distanceToGo,transform.position.y,0);
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         currentLerpTime += Time.deltaTime;
    25.         if (currentLerpTime > lerpTime) {
    26.             currentLerpTime = lerpTime;
    27.         }
    28.  
    29.         float perc = currentLerpTime / lerpTime;
    30.         transform.position = Vector3.Lerp (startPos, endPos, perc);
    31.     }
    32. }
    33.  
     
  9. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    did you try the code i posted above? it should do what you want.
    don't know when or where you want this behaviour if you want to test it just call the Coroutine in Start(). ah and decreasing amount should be a negative number.
    or with lerp maybe something like this:
    Code (CSharp):
    1. speed = Mathf.Lerp(maxSpeed, minSpeed, step);//or make step go from 1 to 0 for the other way around.
    2.  
    3. transform.position = Vector3.MoveTowards(pointA, pointB, Time.deltaTime*speed);
     
    Last edited: Nov 29, 2015
    Like-A-Sir likes this.
  10. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33

    The code with rigidbody didn't work but this one with lerp is better and finally I understand how to use Mathf.Thank you for your help!