- Home /
Adding vector locally for a rocket wobble
Hello, I am attempting to make a rocket that has a nice circular wobble around whichever direction it is flying. I have created a vector3 using sin and cos that can make an object rotate in a circle around it's up axis. However, I don't seem to know how to properly do the math to add this to the rocket, which can be at any arbitrary angle it finds itself in.
My goal is that depending on the rate and magnitude, the rocket will fly more out of control, as if it is damaged or not calibrated correctly, making a corkscrew path.
I left a couple lines of commented out test code in. I'm running out of ideas on how properly implement this. Any guidance would be much appreciated. Thank you.
using UnityEngine;
using System.Collections;
public class wobbleCS : MonoBehaviour {
public float wobbleRate = 10;
public float wobbleMag = 0;
// Update is called once per frame
void FixedUpdate () {
Vector3 rocketWobble = new Vector3(wobbleMag *(Mathf.Sin(Time.time * wobbleRate) ), 0, wobbleMag * (Mathf.Cos(Time.time * wobbleRate) ) );
Debug.Log("Debug: " + rocketWobble.x + ", " + rocketWobble.z + ". ");
//transform.rotation = Quaternion.Euler(rocketWobble); // makes a nice circle around the up axis
//transform.Rotate(Quaternion.Euler(rocketWobble).eulerAngles, Space.Self); //this results in crooked rotations
transform.rotation = Quaternion.Euler(rocketWobble) * transform.rotation; // identical results as the line above I believe
}
}
Did you ever solve this? I really like the effect, but running into the same issue that it will not honor the direction from the spawnpoint. So it always moves in a straight line, no matter what the quaternion was.
Yes, I got it working for my needs. I dug up my old code here:
this.GetComponent<Rigidbody>().AddRelativeForce( (new Vector3(crooked$$anonymous$$ag, 1, 0)) * force); //crooked$$anonymous$$ag deter$$anonymous$$es how off-kilter the rocket should be, force decides how hard the thruster is pushing
this.GetComponent<Rigidbody>().AddRelativeTorque(Vector3.up * spinRate); //makes the rocket spin
I add a relative force upwards, but make it slightly crooked with a variable called crooked$$anonymous$$ag. I also add a spin using relative torque. This results in it spinning and wobbling. I hope this helps.
Answer by jackmw94 · Nov 11, 2020 at 09:55 PM
I think this would be much easier if instead of calculating the actual angles that rocket will be at, you make use of the gameobject hierarchy.
These two screenshots show an example of the setup, the root object has a regular spin around its y-axis and the model object below has a constant x-axis offset:
This is a good solution. I will add that it's important how the force is applied. Since the root is spinning, the model's position values should not change or your results would quickly become bizarre. So the rigid body could be placed on the root node and you could AddForce using the up vector of your model instead of the root. It's also worth noting that the rotation in the rotation script should be done using AddTorque rather than regular rotation because this is physics based.
Your answer
Follow this Question
Related Questions
transform.eulerAngles of camera - changing the speed of rotation not working? 1 Answer
How do I make create a rotateable object that follows a raycast hit point? 2 Answers
Turn and Move Foward 1 Unit relative to Objects local direction 1 Answer
How to prevent Transform from going through walls? 2 Answers
Rotate some coordinates in relation to an object's rotation 0 Answers