[SOLVED] How to make a shake effect be slower? (also question about Update and Time.deltaTime)

Hello. I have a script that, when attached to an object, adds a shake effect.
It works as intended, but my problem is that it shakes too fast. I can reduce the shake intensity, but that only reduces the shake distance from originPosition, while still keeping the speed.

So is there a way to add a speed option? So that the affected object shakes slower depending on what it’s set to?

Here’s the script in question:

using UnityEngine;
using System.Collections;

public class ObjectShake : MonoBehaviour {

    public float shake_intensity;
    public Vector3 originPosition;

    public bool isShaking = true;

    void Start()
    {
        originPosition = transform.position;
    }

    void Update ()
    {
        if (isShaking)
        {
            transform.position = originPosition + Random.insideUnitSphere * shake_intensity;
        }
    }
}

Also, while I’m at it and if anyone feels like answering this. I am still not very knowledgeable about this stuff, so I was wondering about one of my scripts that rotates and moves an object towards a certain point.

The function is within Update(), so, as far as I understand, it’s affected by the framerate, right? Now, what if the player has a slower machine and thus their framerate is not only slower but also less stable? Would the object still move and rotate to the target point without issue and at the same rate due to Time.deltaTime?

Here’s the code lines:

        if (rotate01bool)
        {
            targetPoint = new Vector3(target01.transform.position.x, affectedObject.transform.position.y, target01.transform.position.z) - affectedObject.transform.position;
            targetRotation = Quaternion.LookRotation(targetPoint, Vector3.up);
            affectedObject.transform.rotation = Quaternion.Slerp(affectedObject.transform.rotation, targetRotation, Time.deltaTime * 5);
        }

        if (target01bool)
        {
            float step = 1.1f * Time.deltaTime;
            affectedObject.transform.position = Vector3.MoveTowards(affectedObject.transform.position, target01.transform.position, step);
        }

Add a timing/frequency variable to the shake script and when that time is met or exceeded then do the shake algorithm and reset the timer. You will need a start and stop time variable to reset as well as a variable to hold elapsed time since timer was reset and started again.

For your shaking, right now it is changing position on every frame. Essentially there is nothing for you to set because the functionality you are looking for has not been implemented in your script.

You need to use Vector3.MoveTowards to gradually move the object to the randomly set target position. Only upon reaching the target position, will a new target position be set for the object to move to again. By using movetowards you can set the rate of movement(just like you have for rotation in your second question) which would decide how “Shaky” the object will be.

As for your other question, you should not have anything to worry about as long as whatever you are affecting is multiplied by Time.deltaTime. think of multiplying by deltaTime as X per second. so if i set this rotation for example:

360 * Time.deltaTime;

what i am saying above is, rotate at a rate of 360 degrees in 1 second.

2 Likes

Awesome, that shed some light on it.

I combined my two codes into one and came up with this:

using UnityEngine;
using System.Collections;

public class ObjectShake : MonoBehaviour
{
    public float shake_speed;
    public float shake_intensity;
    public Vector3 originPosition;

    public bool isShaking = true;

    void Start()
    {
        originPosition = transform.position;
    }

    void Update()
    {
        if (isShaking)
        {
            float step = shake_speed * Time.deltaTime;
            transform.position = Vector3.MoveTowards(transform.position, originPosition + Random.insideUnitSphere, step);
        }
    }
}

It seems to work perfectly fine. I hope there’s no issues that might arise from it?

If not, I thank you for your help!

2 Likes

Good work. Give yerself a gold star:)