#Code Bug

1 messages · Page 1 of 1 (latest)

north mural
#

I need help with a hand script I made, its supposed to be mainly like "Granny" where theres a delay with rotation but not position. It only works for random models and I don't know why. It's supposed to be an empty object as the child of the camera and converts the motion to a world space position, then convert it back. Here's the script for anyone who can help:

using UnityEngine;

public class HandScript : MonoBehaviour
{
    [Header("Delay Settings")]
    [Tooltip("How quickly the hand catches up to the camera rotation.")]
    public float rotationLagSpeed = 5f;

    [Tooltip("Optional offset from the camera's rotation.")]
    public Vector3 rotationOffsetEuler = Vector3.zero;

    private Transform cameraRoot;
    private Quaternion currentWorldRotation;

    void Start()
    {
        cameraRoot = Camera.main.transform; // top-level camera
        currentWorldRotation = transform.rotation; // store initial world rotation
    }

    void LateUpdate()
    {
        if (cameraRoot == null) return;

        // Target rotation in world space: camera's world rotation + offset
        Quaternion targetWorldRotation = cameraRoot.rotation * Quaternion.Euler(rotationOffsetEuler);

        // Smoothly interpolate in world space
        currentWorldRotation = Quaternion.Slerp(currentWorldRotation, targetWorldRotation, rotationLagSpeed * Time.deltaTime);

        // Apply the lagged rotation in world space
        transform.rotation = currentWorldRotation;
    }
}
quasi mirage
#

for the ones where it doesn't work, how exactly doesn't it work

#

do you get an error, etc

north mural
#

It only rotates, It should be like a hand following the camera rotation with a delay. So it does change the position, just not late to the cameras transform. The broken ones don't change the position, only rotation.

quasi mirage
#

where are you setting the position

north mural
#

Theres no position change, but it does it on some models even with out that. I've tried adjusting the mesh pivot to see if thats why, but that didn't work. It only looks like the position changes.

quasi mirage
#

i think i'm misunderstanding

#

should the position be changing?

north mural
#

Yes, it looks like it with certain models, like swords, but cubes and others it doesn't

quasi mirage
#

is the hand parented to anything