- Home /
Object Rotate and move towards coordinates
Hi guys, I ran into a problem.
I have this game object that rotates randomly by using this code (transform.rotate(Vector3(random.ranges)). Then, i make this object move on the z axis while rotating, with transform.position.z += value. The thing is i would like it move from whatever point he is currently (he can spawn randomly on different x/y coordinates) towards the camera.
I've tried to check if the value is greater or lesser than the x value i'm looking after and to make adjustments, but it seems the transform rotate also interferes with its x/y/z position, so this lead to very strange animation and behaviour. I would like to know how the rotate values are related to the position values. And i would also like to make it come towards the let's say 0.0 (x/y) position. How can i achieve that?
Many thanks!
EDIT: I think there should be a way to rotate an object on his own coordinates and make it move on the game scene coordinates. These should be different values don't they? Is it possible? Does unity work this way? Does it have different set of coordinates?
Answer by robertbu · May 14, 2014 at 03:06 PM
It is hard to pinpoint the exact code you need/want. Try this:
transform.position = Vector3.MoveTowards(transform.position, Camera.main.transform.position, Time.deltaTime * speed);
'speed' is a variable you define and is in units per second.