Rotating a Game Object Then Applying Force Towards Rotated Direction
I am trying to mimic the natural flight of a frisbee.
Right now I have three scripts, one applies forward force to the disc, one tilts the disc and another applies force in the tilted direction.
The issue that I am having is that when the player throws the disc from one side of the level it acts as it should (flys forward then eventually fades LEFT and looses altitude). However if I throw the disc from the opposite side of the level, it flys forward but this time tilts RIGHT and applies force in that direction (so the disc tilts right and then looses altitude.
I think my issue is coming in when I am trying to Lerp the disc. My vague comprehension is that the disc is perhaps rotating in world space as opposed to self space, but everything I have tried so far will not make it rotate properly.
ThrowDisc.cs is attached to my player game object. TiltDisc.cs and ApplyTiltForce.cs are attached to my instantiated disc game object.
ThrowDisc.cs
public class ThrowDisc : MonoBehaviour
{
public GameObject discHolster;
public float throwForce = 40f;
private GameObject cachedDisc;
void Start()
{
discHolster.transform.rotation = default;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
ThrowRBDisc();
}
}
public void SetDisc(GameObject discToAdd)
{
cachedDisc = discToAdd;
}
public void ThrowRBDisc()
{
GameObject disc = Instantiate(cachedDisc, transform.position, transform.rotation);
Rigidbody rb = disc.GetComponent<Rigidbody>();
rb.AddForce(transform.forward * throwForce, ForceMode.VelocityChange);
}
}
TiltDisc.cs
public class TiltOutDisc : MonoBehaviour
{
public float transitionSpeed = 1f;
Vector3 tiltOutVector = new Vector3(0, 0, 45);
private Rigidbody _discRb;
void Start()
{
_discRb = GetComponent<Rigidbody>();
}
void Update()
{
if (!_didCollide)
{
if (_discRb.velocity.magnitude < 40f) TiltOut();
}
}
void TiltOut()
{
Debug.Log("Tilting Out");
Vector3 currentAngle = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, tiltOutVector.x, Time.deltaTime * transitionSpeed),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, tiltOutVector.y, Time.deltaTime * transitionSpeed),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, tiltOutVector.z, Time.deltaTime * transitionSpeed));
transform.eulerAngles = currentAngle;
}
}
ApplyTiltForce.cs
public class ApplyTiltForce : MonoBehaviour
{
private Rigidbody _discRb;
public float divideBy = 6f;
private float _currentZ;
private float _multipliedForce = 0f;
void Start()
{
_discRb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
ApplyConditionalAngle();
}
void ApplyConditionalAngle()
{
/// If disc is leaning to the left.
if (transform.eulerAngles.z >= 1f && transform.eulerAngles.z <= 60f)
{
/// The discs z-axis value to _currentZ.
_currentZ = transform.eulerAngles.z;
/// _currentZ needs to be a lower value so lets divide it before applying
/// as a force value.
_currentZ = _currentZ / divideBy;
/// Apply force in the form of the z-axis value to the x-axis of
/// the disc. Multiply force if preferred.
_discRb.AddForce(-_currentZ, 0f, 0f * _multipliedForce);
}
/// If disc is leaning to the right.
if (transform.eulerAngles.z <= 360f && transform.eulerAngles.z >= 300f)
{
/// Returns a negative value
_currentZ = transform.eulerAngles.z - 360f;
/// Converts negative value into a positive value.
_currentZ *= -1f;
/// Lower the value by dividing it.
_currentZ = _currentZ / divideBy;
/// Apply calculated force to x-axis * _multipliedForce.
_discRb.AddForce(_currentZ, 0f, 0f * _multipliedForce, ForceMode.Acceleration); // Default
}
}
}
Any help would be immensely appreciated, please let me know if I left out any information that might help solve this issue.
Your answer
Follow this Question
Related Questions
Movement issue, the player accelerates but does not reduce in speed when hitting a wall or object 1 Answer
Making object move up and down in Y axis while rotating down and up 1 Answer
Rotate and object with Touch 0 Answers
3D First Person Game, 2D Enemy Face Player/Camera 1 Answer
How come when I rotate my spaceship, it also rotates on the two other axis's? Please help? 0 Answers