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

Add force and stop when it gets to the assigned position

Discussion in 'Editor & General Support' started by gab123, Sep 18, 2016.

  1. gab123

    gab123

    Joined:
    Mar 6, 2016
    Posts:
    14
    Hi,
    I am trying to make a game like a color switch in unity and I have a problem :
    When user clicks space I want a player object which is a sphere to jump few inches higher and stop there.
    I was trying to use righidbody.addForce() but when I call it the player object just keept going up and doesnt stop
    How can i stop add force when player object gets ti the assigned position ?
    Please help.
     
  2. SuperVoximus

    SuperVoximus

    Joined:
    Nov 5, 2013
    Posts:
    102
    Hmm, do you need to use addforce? Or is translating okay?

    You could store the current player's position in a Vector3 variable, and create a boolean variable. The boolean variable would be false until you reached your desired position.

    Example (Untested Code):


    Code (JavaScript):
    1. var moving : boolean = false;
    2. var currentPosition : Vector3;
    3. var destinationPosition : Vector3;
    4.  
    5. function Update(){
    6.  
    7. currentPosition = Transform.position;
    8.  
    9. if( moving == true){
    10.    transform.Translate(Vector3.forward * Time.deltaTime); //use whatever movement code here
    11. }
    12.  
    13.    if( Input){ //however you trigger movement
    14.     destinationPosition = Vector3( currentPosition.x+1, currentPosition.y, currentPosition.z); //example adding +1 x
    15.     moving = true;
    16. }
    17.  
    18.    if( currentPosition = destinationPosition){
    19.    moving = false;
    20. }
    21.  
    22. }
    The reason I asked about using addforce is I think addforce will continue to move you a little bit even when you stop it, whereas translation stops immediately. Hope this helps!
     
  3. gab123

    gab123

    Joined:
    Mar 6, 2016
    Posts:
    14
    I changed my code and now it looks like this :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     private Vector3 aktualposition ;
    8.     public GameObject camera ;
    9.     private Vector3 difference ;
    10.  
    11.     void start(){
    12.         difference = camera.transform.position - transform.position;
    13.     }
    14.     void FixedUpdate () {
    15.  
    16.         bool moving = false;
    17.         Vector3 currentPosition;
    18.         Vector3 destinationPosition ;
    19.        
    20.             currentPosition = transform.position;
    21.             destinationPosition = new Vector3( currentPosition.x, currentPosition.y+1, currentPosition.z);
    22.  
    23.         if (moving == true) {
    24.             transform.Translate (Vector3.forward * Time.deltaTime); //use whatever movement code here
    25.         } else {
    26.             camera.transform.position = transform.position + difference;
    27.         }
    28.            
    29.             if( Input.GetButtonDown("Jump")){ //however you trigger movement
    30.                 destinationPosition = new Vector3( currentPosition.x+1, currentPosition.y, currentPosition.z); //example adding +1 x
    31.                 moving = true;
    32.             }
    33.            
    34.             if( currentPosition == destinationPosition){
    35.                moving = false;
    36.             }
    37.            
    38.         }
    39.        
    40. }
    41.  
    42.  
    And righ now after I click space nothing happens. It doesnt move at all
     
  4. SuperVoximus

    SuperVoximus

    Joined:
    Nov 5, 2013
    Posts:
    102
    In the Start() function put this :
    destinationPosition = Vector3( currentPosition.x, currentPosition.y, currentPosition.z);


    And try removing line 16, 17 and 21.

    Also try and do
    destinationPosition = Vector3 rather than new Vector3 when you press space.
     
  5. gab123

    gab123

    Joined:
    Mar 6, 2016
    Posts:
    14
    Right now when i click space the z- position keeps increasing and doesnt stop untill I exit the game.