- Home /
Rotating a football with a spiral...
Hello,
I have a football I'd like to throw and while it is in the air to have its upward direction pointing in the way of its velocity, while rotating (spiraling) around that axis.
Here is my code that points it to the velocity, but not rotating spiral: void Update () { if (thrown) { model.up = body.velocity; model.Rotate(Vector3.up, 500 * Time.deltaTime); } }
Thank you community!
Answer by toddisarockstar · Jun 10, 2017 at 09:17 PM
assuming you modeled your football with the ends up and down. you would only apply the velocity to x and z. and spin on the y axis.
this should work if you put it in update:
float spin;
float spinspeed;
Vector3 facing;
Rigidbody rb;
void Start(){
rb = GetComponent<Rigidbody>();
spin = 1f;
spinspeed=20f; //adjust for spin speed of ball
}
void Update () {
facing=Quaternion.LookRotation(rb.velocity).eulerAngles;
spin += Time.deltaTime * spinspeed;
if(spin>360f){spin=spin-360f;}
transform.eulerAngles = new Vector3 (facing.x, spin, facing.z);
// if you modeled the ball left and right use this instead:
// transform.eulerAngles = new Vector3 (spin, facing.y, facing.z);
// if you modeled the ball forward and back use this instead:
// transform.eulerAngles = new Vector3 (facing.x, facing.y, spin);
}
Answer by shadowpuppet · Jun 12, 2017 at 09:13 AM
why not just have an animation component on the football that loops itself spiraling
Answer by tMahon · Jun 12, 2017 at 09:14 AM
You could also use a WheelCollider Component attached to the football and just add motor torque to it. It seems a little counter-intuitive, but if you're like me, and you don't like working with Quaternions, and you have a Rigidbody on your football, this is a good workaround. It's really quite simple, just add a wheel collider component to an empty GameObject child to the Football with its Mesh Collider or whatever it's using. From there just add motor torque to the wheel.
using UnityEngine;
public class ThrowFootball : MonoBehaviour {
[SerializeField]
WheelCollider spiralCollider;
[SerializeField]
MeshRenderer footballMesh;
[SerializeField]
float throwTorque = 100f;
// Use this for initialization
void Start () {
ThrowBall();
}
private void ThrowBall()
{
spiralCollider.motorTorque = throwTorque;
gameObject.GetComponent<Rigidbody>().AddForce((transform.up - transform.forward));
}
// Update is called once per frame
void Update () {
Vector3 pos = new Vector3();
Quaternion quat = new Quaternion();
spiralCollider.GetWorldPose(out pos, out quat);
footballMesh.transform.position = pos;
footballMesh.transform.rotation = quat;
}
}