A
A
Alexey2017-05-09 21:00:03
C++ / C#
Alexey, 2017-05-09 21:00:03

How to move an object to a certain point using rigidbody.velocity?

I have an object that moves with the help of velocity in X and Y.
When you press the button, it should move to a certain Z coordinate.
Here is the code just moves it along the Z coordinate

rigidbody3D = GetComponent<Rigidbody>();
...
rigidbody3D.velocity = new Vector3(rigidbody3D.velocity.x, rigidbody3D.velocity.y, layerLevelPosition[1].z);

It moves as needed, it needs to hold down the button and at the same time the object moves beyond the desired Z coordinate (layerLevelPosition[1].z).
transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x, transform.position.y, layerLevelPosition[1].z),0.5f);

This code moves the object to the desired Z coordinate, but you also need to hold down the button.
1) Is it possible to move to a certain Z coordinate using velocity?
2) Is there a method that itself "moves" the object to the desired point or only through a loop?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Pogrebnyak, 2017-05-09
@Alexeee

1.Mathf.Clamp.
2. Loop/coroutines with Vector3.Lerp.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question