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);
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. =)
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.
Read the docs! if you’re inside the FixedUpdate method, deltaTime will resolve to fixedDeltaTime. So replacing deltaTime with fixedDeltaTime does literally nothing.
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?
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?
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.