Move a cube from one place to other at selected speed
I am new in unity 3d. I am trying to write a Java script to move the cube from one place to other on x axis. I just want to know how to control the speed.
Answer by M-Hanssen · Apr 12, 2016 at 01:44 PM
You have different approaches to achieve this. The most simple one, is to translate the cube's position:
void Update()
{
transform.Translate(0.1f, 0, 0);
}
Where 0.1f is the speed over the X-axis
Its working. Thanks Hassan. But How to stop it? I just want to move it from point A to point B
To move from point A to point B you could use another approach:
Please read the Vector3.$$anonymous$$oveTowards documentation to get familiar with this concept.
Your code could look something like this:
/// <summary>
/// The target transform to move to
/// </summary>
public Transform Target;
/// <summary>
/// The speed this object will move towards the target
/// </summary>
public float Speed = 10;
protected void Update()
{
float step = Speed * Time.deltaTime;
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, Target.position, step);
}
$$anonymous$$ake sure you fill the Target transform in your inspector
Answer by 5c4r3cr0w · Apr 12, 2016 at 01:52 PM
Don't know how it's done in Javascript. But some code in c#
transform.position += transform.right * Time.deltaTime * mul;
change the value of 'mul' until you get desired speed.
Answer by Unity_scat · Apr 12, 2016 at 02:41 PM
If the speed is slow, you can move the object, set a delay with yield, then repeat the process.
Your answer
Follow this Question
Related Questions
Any way to use UI on a screen renderer? 0 Answers
addComponent c# script by java 0 Answers
Jump Issue :( 1 Answer
SHA512 JS -> C# 0 Answers