0
\$\begingroup\$

The reason why I need to use a coroutine for the rotation is that the object I throw to the target will move to the target and back once and then when the object return back I want the object also to be rotating facing forward but to be rotating smoothly slowly. first, I want the object to look at the target rotate to the target smooth slowly in the Throw method then in the ThrowBack method to rotate facing the parent.

I'm using coroutines because I want to make things once. for example, move to the target and back and then rotate but if I will make the rotation in the update it will keep rotating and I need the object to be able to rotate later too so I think it's better to make the rotation in a coroutine.

   using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ThrowObject : MonoBehaviour
    {
        public Transform objectToThrow;
        public Transform target;
        public Transform objectToThrowParent;
        public float throwingSpeed;
        public bool go = false;
    
        private Animator anim;
        private bool startThrowAnimationOnce = true;
        private Vector3 originPos;
    
        private void Start()
        {
            anim = GetComponent<Animator>();
            originPos = objectToThrowParent.position;
        }
    
        private void Update()
        {
            if (anim != null && startThrowAnimationOnce))
            {
                anim.SetTrigger("Throw");
    
                startThrowAnimationOnce = false;
            }
    
            if (go)
            {
                objectToThrow.parent = null;
    
                StartCoroutine(Throw());
    
                go = false;
            }
        }
    
        public void ThrowEvent()
        {
            go = true;
        }
    
        IEnumerator Throw()
        {
            while (objectToThrow.position != target.position)
            {
                objectToThrow.position = Vector3.MoveTowards(
                      objectToThrow.position,
                      target.position,
                      throwingSpeed * Time.deltaTime
                 );
    
                yield return null;
            }

            objectToThrow.position = target.position;
    
            yield return new WaitForSeconds(10f);
    
            StartCoroutine(ThrowBack());
        }
    
        IEnumerator ThrowBack()
        {
            while (objectToThrow.position != objectToThrowParent.position)
            {
                objectToThrow.position = Vector3.MoveTowards(
                      objectToThrow.position,
                      objectToThrowParent.position,
                      throwingSpeed * Time.deltaTime
                 );
                yield return null;
            }
            objectToThrow.position = objectToThrowParent.position;
    
            objectToThrow.parent = objectToThrowParent;
            objectToThrow.localPosition = new Vector3(0, 0, 0);
            //objectToThrow.localRotation = Quaternion.Euler(0, 0, 0);
        }
    }
```
\$\endgroup\$
1
  • \$\begingroup\$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. \$\endgroup\$
    – Community Bot
    Mar 22 at 10:42

1 Answer 1

1
\$\begingroup\$

First of all, don't use (objectToMove.position != objectToMoveParent.position) instead try Vector3.Distance(objectToMove.position, objectToMoveParent.position) >= 0.1f because there's always a small distance between the two points.

And you don't need any coroutine to smoothly rotate the object, just use this line: GetComponent<Rigidbody2D>().MoveRotation(Quaternion.RotateTowards(from, to, maxDegreesDelta)); (or Vector3.RotateTowards). from and to are two quaternions which determine the start angle and the end angle; maxDegreesDelta means how many degrees per second the object rotates (don't forget to use Time.deltaTime), or in other words, controls the speed of rotation.

Here are the links to unity documentation if you need to look: RigidBody.MoveRotation and Vector3.RotateTowards

\$\endgroup\$
2
  • \$\begingroup\$ @MuhamadBanguzi "there is a reason why i need to use a coroutine", so why not include this information in your question rather than in comments? By now, all people see is source code and nothing else. Please edit your question and add details about what you're trying to achieve, and what you got so far. \$\endgroup\$
    – liggiorgio
    Mar 22 at 10:40
  • \$\begingroup\$ Keep in mind that the == / != operators for Vector3 values in Unity already includes a tolerance of about 0.0001 units, so the normal warnings about not comparing for exact equality don't apply. \$\endgroup\$
    – DMGregory
    Mar 22 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .