- Home /
Question is off-topic or not relevant
Make an object move forward on its own c#
hello,
i want to make object move on its own with c#
thanks,
Try sticking this in one of your object's scripts, that will move it along its local z axis:
void Update()
{
// ...
transform.localPosition += transform.forward * speed * Time.deltaTime; // or transform.position, both would work
// ...
}
what do I do if I want this to do for only a few seconds
Answer by Doireth · Oct 12, 2013 at 02:28 PM
transform.position += Vector3.forward * Time.deltaTime;
Actually, that will move it along the global z axis, not the object's local.
The question didn't specify which axis, just that it moves.
Answer by Prasanna · Feb 19, 2014 at 06:03 AM
@vexe. Sorry for late Reply....
public class CharacterMovement : MonoBehaviour
{
// Player Movement Variables/....
public static int movespeed = 1;
public Vector3 userDirection = Vector3.right;
public Start()
{
}
public void Update()
{
transform.Translate(userDirection * movespeed * Time.deltaTime);
}
}
-Prasanna
Answer by Tomer-Barkan · Oct 12, 2013 at 03:57 PM
You can either move the object manually every frame by changing the transform.position, but if you want it to move "by itself", based on physics, then you add a rigidbody component and give it some velocity:
rigidbody.velocity = transform.forward;
I warmly recommend starting with basic physics and scripting tutorials before asking questions because these basic concepts are well covered there:
http://unity3d.com/learn/tutorials/modules/beginner/physics/rigidbody
http://unity3d.com/learn/tutorials/modules/beginner/scripting/translate-and-rotate
Updated to new API
GetComponent<Rigidbody> ().velocity = transform.forward;
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Camera will not move between given points 1 Answer
Input.GetAxis not returning to zero after key is released 0 Answers