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

MovePosition acts like transform position

Discussion in 'Scripting' started by JoeGerman, Jan 7, 2015.

  1. JoeGerman

    JoeGerman

    Joined:
    Jan 7, 2015
    Posts:
    3
    I'm sure this is a total noob question but I'm trying to make an object randomly move around a closed space. The object randomly chooses a point within a set of boundaries and then heads to the point. Once there, it selects a new point and then heads to the new point accordingly. The problem is, it just jumps from place to place as if its position was transformed as opposed to using rigidbody.MovePosition (which I am doing). Does anybody know what mistake I'm making in my code. Thanks.

    Note: the speedVector variable has no effect on the speed regardless of how I manipulate the speed variable.


    public class PickupRunner : MonoBehaviour {

    public float speed = 0.1f;
    private Vector3 direction;

    private float westBounds = -18f; //sets the boundarys of the walls
    private float eastBounds = 18f; //to which the object will move
    private float northBounds = 18f;
    private float southBounds = -18f;

    void Start()
    {
    direction = (new Vector3(Random.Range (westBounds,eastBounds),1f, Random.Range (southBounds,northBounds)));
    transform.position = direction;
    }

    void FixedUpdate()
    {

    Vector3 speedVector = new Vector3(speed, 0f, speed);

    rigidbody.MovePosition(direction + speedVector * Time.deltaTime);

    if (transform.position == (direction + speedVector * Time.deltaTime)) {

    direction = (new Vector3(Random.Range (westBounds,eastBounds),1f, Random.Range (southBounds,northBounds)));

    }
    }
    }
     
  2. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    MovePosition moves the object to the location given between updates, so you won't see it happening.
    Something like rigidbody.AddForce is what you'll want to use if you want it controlled by physics.
    As a side note, you'll likely get more responses if you use the code tags. =)
     
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    They are similar.

    This works great for me. Input a control input vector (like, Horizontal & Vertical) in FixedUpdate.
    Code (csharp):
    1.  
    2. void DoMovement()
    3.         {
    4.             rb.MovePosition (go.transform.position + inputVector * multiplier);
    5.         }
     
  4. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    If you normalize your current position subtracted from your target position, I think that will give you a direction to move in. I might have that backwards.
     
  5. arthur_fukushima

    arthur_fukushima

    Joined:
    Nov 12, 2014
    Posts:
    28
    Just an observation, since you are using the FixedUpdate method, try to replace the Time.deltaTime for Time.fixedDeltaTime.

    Can't say if this will improve something, but since you are the position of a object with a rigidbody, try something like this:

    Code (CSharp):
    1. rigidbody.MovePosition(direction + speedVector * Time.fixedDeltaTime);
    2.  
    3. if (rigidbody.position == (direction + speedVector * Time.fixedDeltaTime))
    4. {
    5. //DO THINGS...
    6. }
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,184
    Read the docs! if you're inside the FixedUpdate method, deltaTime will resolve to fixedDeltaTime. So replacing deltaTime with fixedDeltaTime does literally nothing.
     
  7. JoeGerman

    JoeGerman

    Joined:
    Jan 7, 2015
    Posts:
    3
    I actually use AddForce on a bouncing object of mine in another script, but what if I want an object to move from one position to another specific position, but still would want to see the movement take place? I shouldn't use rigidbody.MovePosition?
     
  8. JoeGerman

    JoeGerman

    Joined:
    Jan 7, 2015
    Posts:
    3
    So in this case, would the input vector be the new position that I want the gameObject to move towards, with the multiplier being speed * Time.deltaTime?
     
  9. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    Its the input + the position. So if the player presses Left the input will change from [0.0] to [-1.0] and you can just add that to the position. through .MovePosition. The multiplier is like a speed variable.

    [edit] doh... sorry I just realized you were looking for a destination/AI.... This would be for a player.
     
    Last edited: Jan 8, 2015