- Home /
How to make a gamebject wiggle slowly as it moves forward ?
Hello,
I`m wondering how do i make my gameobject move like a snake while going forward.I have a rocked and i want it to mimic that movement while it moves.
Thanks :)
There are so many possible answers to this question, from scripting, through physics, to animation, and more. If you give more details, tells us more about your project and what you are using at the moment, you are more likely to get an answer that gets you what you want.
Anyway, as I'm most comfortable with scripting, I would add a script like this (it's a bit of hack, because it depends on original position):
public class Wiggling$$anonymous$$ove : $$anonymous$$onoBehaviour {
[SerializeField] private float forwardSpeed = 1.0f;
[SerializeField] private float wiggleAmplitude = 0.1f;
[SerializeField] private float wiggleFrequency = 1f;
Vector3 originalPosition;
private float t = 0;
private void Start () {
originalPosition = transform.position;
}
private void Update () {
t += Time.deltaTime;
Vector3 displacement = transform.forward * forwardSpeed + transform.right * $$anonymous$$athf.Sin(wiggleFrequency * t) * wiggleAmplitude;
transform.position = originalPosition + displacement;
}
}
Answer by OrionGamesInc · Apr 27, 2018 at 11:42 AM
You can get the best results from using an attached Rigidbody component and add force: https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
If you want to make the rocket start at a speed instantly you can set the Rigidbody's velocity to the desired speed when it's instantiated or when the game starts. https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html