- Home /
2D - Move an object
I'm trying to implement a function which allow IA to move an object randomly (smoothly, NOT like from a point to point directly without seeing that the object is moving).
My question is if I'm coding movements right. This is just the movement parts which I'm having doubts about:
//Moving towards right
transform.position += transform.right * movementSpeed * Time.deltaTime;
//Moving towards left
transform.position -= transform.right * movementSpeed * Time.deltaTime;
//Moving upwards
transform.position += transform.up * movementSpeed * Time.deltaTime;
//Moving downwards
transform.position -= transform.up * movementSpeed * Time.deltaTime;
I'm asking this question, because seems that after few movements, the object is starting to make small movements in random direction (like if the transform.position is = 1).
If it can help, here the function complete:
public class Movement_Worker : MonoBehaviour
{
public float movementSpeed;
public int limiteStessaDirezione = 15;
bool UP = false;
bool DOWN = false;
bool RIGHT = false;
bool LEFT = false;
bool FERMO = false;
//sceltaMovimento indicates if we can choose a new direction or not
bool sceltaMovimento = true;
//i_tipoMovimento is just a counter.
int i_tipoMovimento = 0;
void FixedUpdate()
{
sceltaMov();
}
private void sceltaMov()
{
Debug.Log("tipoMovimento = " + sceltaMovimento);
int scelta = Random.Range(1, 6);
if (i_tipoMovimento >= limiteStessaDirezione)
{
sceltaMovimento = true;
i_tipoMovimento = 0;
}
if (sceltaMovimento == true)
{
switch (scelta)
{
case 1:
//RIGHT
transform.position += transform.right * movementSpeed * Time.deltaTime;
RIGHT = true;
sceltaMovimento = false;
break;
case 2:
//LEFT
transform.position -= transform.right * movementSpeed * Time.deltaTime;
LEFT = true;
sceltaMovimento = false;
break;
case 3:
//UP
transform.position += transform.up * movementSpeed * Time.deltaTime;
UP = true;
sceltaMovimento = false;
break;
case 4:
//DOWN
transform.position -= transform.up * movementSpeed * Time.deltaTime;
DOWN = true;
sceltaMovimento = false;
break;
default:
//DONT MOVE
//transform.position = transform.position;
FERMO = true;
sceltaMovimento = false;
break;
}
}
//Repeat same movement X times
while(sceltaMovimento == false)
{
if (FERMO == true)
{
i_tipoMovimento += 1;
break;
}
else if (RIGHT == true)
{
i_tipoMovimento += 1;
transform.position += transform.right * movementSpeed * Time.deltaTime;
break;
}
else if (LEFT == true)
{
i_tipoMovimento += 1;
transform.position -= transform.right * movementSpeed * Time.deltaTime;
break;
}
else if (UP == true)
{
i_tipoMovimento += 1;
transform.position += transform.up * movementSpeed * Time.deltaTime;
break;
}
else if (DOWN == true)
{
i_tipoMovimento += 1;
transform.position -= transform.up * movementSpeed * Time.deltaTime;
break;
}
}
}
}
Answer by VeryAnnoyingCat · Oct 14, 2020 at 05:41 PM
I think that you are trying to move a gameobject in a certain direction. This script may be useful:
public float direction = 45; //in degrees (plus 270 degrees)
public float speed = 2; //in Units per second
private float directionInRadians;
private Vector3 movementPerTimestepVector;
void FixedUpdate()
{
directionInRadians = direction * Mathf.Deg2Rad;
movementPerTimestepVector = new Vector3(Mathf.Cos(directionInRadians), Mathf.Sin(directionInRadians), 0) * speed * Time.deltaTime;
transform.position += movementPerTimestepVector;
}
It moves the gameobject in a certain direction at a fixed speed. You can set the direction to random.
I tried (and re arrange) your script. If I randomize the direction (from 0 to 360), 2d object start moving like a "mosquito" (random every frame) and is not what I'm trying to achieve. I'd like to maintain a certain random direction for a bit (like in a more human way) and maybe to stop for a while (randomly).
The problem with my script above is that after few change in direction (for eg. object move a bit to the right, then a bit to the top ecc...), object start moving JUST one step in every random position every frame, like a "robot mosquito" (sorry for the example). How can I achieve the correct random movement?
Ok using your script I finished with this script which is working as I expected:
public float direction = 45; //in degrees (plus 270 degrees)
public float speed = 8; //in Units per second
public int directionCounter = 0;
private float directionInRadians;
private Vector3 movementPerTimestepVector;
private bool youCan$$anonymous$$ove = true;
void FixedUpdate()
{
if(directionCounter == 50)
{
//Change random direction
direction = Random.Range(0, 360);
directionCounter = 0;
//Change the flow of function between moving or stop
if (youCan$$anonymous$$ove == true)
youCan$$anonymous$$ove = false;
else youCan$$anonymous$$ove = true;
}
if(youCan$$anonymous$$ove == true)
{
//$$anonymous$$oving in ONE random direction
directionCounter++;
directionInRadians = direction * $$anonymous$$athf.Deg2Rad;
movementPerTimestepVector = new Vector3($$anonymous$$athf.Cos(directionInRadians), $$anonymous$$athf.Sin(directionInRadians), 0) * speed * Time.deltaTime;
transform.position += movementPerTimestepVector;
}
else
{
//Dont move here
directionCounter++;
}
}
Thank you very much for your help!
It's good that you solved your problem, but I wouldn't recommend that you use frames to time it, not even inside FixedUpdate. Use seconds.
Try this instead:
public float speed = 8; // in Units per second
public float directionAngle = 0; // in degrees (plus 270 degrees)
public float directionDuration = 1; // Seconds to wait before changing direction
private float directionTimeRemaining; // Seconds remaining
void FixedUpdate()
{
directionTimeRemaining -= Time.deltaTime;
if (directionTimeRemaining <= 0)
{
// Change random direction
directionTimeRemaining = directionDuration;
directionAngle = Random.Range(0, 360);
}
// $$anonymous$$ove
transform.position += Quaternion.Euler(0, 0, -directionAngle) * Vector3.right * speed * Time.deltaTime;
}
Your answer
Follow this Question
Related Questions
Would i need Time.deltaTime if am just incrementing transform.position 1 Answer
How to fix box drop via code animation? 2 Answers
Transform player to RaycastHit.point depending on speed float 1 Answer
Generating an object in a random y position? c# 1 Answer
moving objects with transform position 2 Answers