0
\$\begingroup\$

This is the part when i'm trying to rotate the objectToThrow smooth slowly.

objectToThrow.LookAt(playerHead);
                //objectToThrow.rotation = Quaternion.Slerp(objectToThrow.rotation, playerHead.rotation, 3f * Time.deltaTime);

When i'm using the LookAt(playerHead) it does make the rotation but at once fast. When i'm using:

objectToThrow.rotation = Quaternion.Slerp(objectToThrow.rotation, playerHead.rotation, 3f * 
 Time.deltaTime);

It's not rotating at all and when using the Slerp in runtime and try to rotate the objectToThrow in the editor there is some force that not let me rotate it. I can't figure out why the LookAt is working but the Quaternion.Slerp is not ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThrowObject : MonoBehaviour
{
    public Transform objectToThrow;
    public Transform target;
    public Transform playerHead;
    public Transform objectToThrowParent;
    public Animator objectToThrowAnimator;
    public float throwingSpeed;
    public float waitAtTargetTime;
    public float distanceToStopFromTarget;
    public bool go = false;
    public AnimationCurve animationCurve;
    public Hovl_DemoLasers laser;

    private Animator anim;
    private bool startThrowAnimationOnce = true;
    private bool reParent = false;
    private bool startMovingBack = false;
    private bool startRot = false;

    private void Start()
    {
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
        if (anim != null && startThrowAnimationOnce)
        {
            anim.SetTrigger("Throw");

            startThrowAnimationOnce = false;
        }

        if (go)
        {
            objectToThrow.parent = null;
            StartCoroutine(Throw());
            var objtothrow = objectToThrow.GetComponent<InteractableItem>();
            objtothrow.enabledInteraction = true;
            objtothrow.distance = 100;
            target.GetComponent<InteractableItem>().enabledInteraction = false;
            objectToThrow.LookAt(target);
            go = false;
        }

        if (reParent)
        {
            objectToThrow.position = objectToThrowParent.position;
        }

        if (startMovingBack)
        {
            if (Vector3.Distance(objectToThrow.position, playerHead.position) >= 1f)//0.1f)
            {
                float step = 5 * Time.deltaTime;
                objectToThrow.position = Vector3.MoveTowards(objectToThrow.position, playerHead.position, step);

                startRot = true;
            }
        }

        if(startRot)
        {
            objectToThrow.LookAt(playerHead);
            //objectToThrow.rotation = Quaternion.Slerp(objectToThrow.rotation, playerHead.rotation, 3f * Time.deltaTime);
        }
    }

    public void ThrowEvent()
    {
        go = true;
    }

    IEnumerator Throw()
    {
        while(Vector3.Distance(objectToThrow.position, target.position) >= distanceToStopFromTarget)
        {
            objectToThrow.position = Vector3.MoveTowards(
                  objectToThrow.position,
                  target.position,
                  throwingSpeed * Time.deltaTime
             );

            yield return null;
        }

        laser.startLaser = true;

        yield return new WaitForSeconds(waitAtTargetTime);

        target.parent = objectToThrow;
        laser.hovl_laserDemo.MaxLength = Vector3.Distance(objectToThrow.position, target.position);
        objectToThrowAnimator.enabled = false;

        startMovingBack = true;
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Looks like you meant to write:

var lookDirection = playerHead.position - objectToThrow.position;

var lookOrientation = Quaternion.LookRotation(lookDirection);

objectToThrow.rotation = Quaternion.Lerp(objectToThrow.rotation, lookOrientation, 3f * 

Time.deltaTime);

playerHead.rotation is the quaternion describing the way the head is facing, not the way another object would need to rotate to face the head. So we need to calculate that orientation quaternion ourselves.

Also, since you're already using a non-linear blending factor anyway, there's no real benefit to using the more expensive Slerp over the cheaper Lerp. If you want an exactly linear rotation rate, use RotateTowards insteadz which will allow you to enforce a particular speed in degrees per second.

\$\endgroup\$
3
  • \$\begingroup\$ The lerp is working. what if i want to rotate it only on the x ? \$\endgroup\$
    – Daniel Lip
    Apr 10, 2022 at 23:06
  • 1
    \$\begingroup\$ You mean pitch up and down around the local x axis, like nodding your head "yes"? That looks like a different question to me. \$\endgroup\$
    – DMGregory
    Apr 10, 2022 at 23:11
  • \$\begingroup\$ you right, i opened a new question for that. \$\endgroup\$
    – Daniel Lip
    Apr 10, 2022 at 23:22

You must log in to answer this question.

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