I am trying to move the object in my frame in Time.deltaTime. I have not taken care the speed yet but the problem is, the object is almost at position (0,0) after I have multiplied the x & y axis with Time.deltaTime. I want it to appear randomly on the screen in a given range. The update function is given below:

function Update () 

  var x = Random.Range(-4.37,4.34) * Time.deltaTime;

  var y = Random.Range(4.45,-4.66) * Time.deltaTime;

  var velocity = Vector3(x, y, 0);

  transform.position = velocity;


You're setting your position equal to your velocity. Your velocity is very small because it's multiplied by delta time. This results in very small changes to your position, constrained to a very small area. Your object is moving, it's just not noticeable because the changes are so small. Additionally, all the small changes are not adding up because you're setting the position each time, instead of adding to it.

What you really want is to change your position with respect to time.

var x = Random.Range(-4.37,4.34);
var y = Random.Range(4.45,-4.66);
var velocity = Vector3(x, y, 0);
transform.position += velocity * Time.deltaTime;

This creates a random velocity, then changes the position of the object based on that velocity.


Sorry I misread your question. I saw your velocity variable and thought you wanted to move the object over time. But your description says you want it to appear at random locations. Simply change this line: transform.position += velocity * Time.deltaTime; to transform.position = velocity; To make it appear at a random location (I'd also suggest renaming the variable from velocity, because that indicates a change in position over time).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.