Shake camera sequence command

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
OneManOnMars
Posts: 105
Joined: Tue Apr 05, 2016 9:37 am

Shake camera sequence command

Post by OneManOnMars »

Hi everyone,

I am looking for a sequence command that shakes the camera. I looked trough the documentation and looked up the forum for it but could not find anyting.

This was a bit of a surprise to me I really tought someone must have had the need for a good cam shake druing their sequence.

So first question? Am I right, that there is no such thing right now?

If thats the case I have to try to get it done myself.

How would I takle it.

in start:
look for the camera via a string Parameter.
string objectName = GetParameter(0);
GameObject camObject = GameObject.Find(objectName);

then in update doshake the camera for some frames.

and end it with a stop(); for clean up.

it would be nice if intensity and duration could be passed into the function via command parameters.

Is that right?

I'll do some test to see if I can get it done somehow. If someone could help me out with hints or tipps, that would be very appreciated!

Best,

Roman
OneManOnMars
Posts: 105
Joined: Tue Apr 05, 2016 9:37 am

Re: Shake camera sequence command

Post by OneManOnMars »

Code: Select all

using UnityEngine;
using System.Collections;
using PixelCrushers.DialogueSystem;

namespace PixelCrushers.DialogueSystem.SequencerCommands {

	public class SequencerCommandCamShake : SequencerCommand {



		// How long the object should shake for.
		public float shakeDuration = 0f;

		// Amplitude of the shake. A larger value shakes the camera harder.
		public float shakeAmount = 0.7f;
		public float decreaseFactor = 1.0f;

		private	Vector3 originalPos;
		private GameObject camObject;

		public void Start() {
			// Add your initialization code here. You can use the GetParameter***() and GetSubject()
			// functions to get information from the command's parameters. You can also use the
			// Sequencer property to access the SequencerCamera, CameraAngle, Speaker, Listener,
			// SubtitleEndTime, and other properties on the sequencer. If IsAudioMuted() is true, 
			// the player has muted audio.
			//
			// If your sequencer command only does something immediately and then finishes,
			// you can call Stop() here and remove the Update() method.
			string objectName = GetParameter(0);
			camObject = GameObject.Find(objectName);
			originalPos = camObject.transform.localPosition;

		}

		void Update()
		{
			if (shakeDuration > 0)
			{
				camObject.transform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;

				shakeDuration -= Time.deltaTime * decreaseFactor;
			}
			else
			{
				shakeDuration = 0f;
				camObject.transform.localPosition = originalPos;
			}
		//	stop();
		}


	

	}

}

This is my first attempt. No errors but doesn't do a thing...

Used this as a guide to shake the cam:
https://gist.github.com/ftvs/5822103
User avatar
Tony Li
Posts: 21070
Joined: Thu Jul 18, 2013 1:27 pm

Re: Shake camera sequence command

Post by Tony Li »

That's pretty close. Try this slight modification. The syntax of the command is:

CamShake(shakeAmount, decreaseFactor, shakeDuration)

For example:

CamShake(0.7, 1, 2)

Code: Select all

using UnityEngine;
using System.Collections;
using PixelCrushers.DialogueSystem;

namespace PixelCrushers.DialogueSystem.SequencerCommands
{

    /// <summary>
    /// Syntax: CamShake(shakeAmount, decreaseFactor, shakeDuration)
    /// </summary>
    public class SequencerCommandCamShake : SequencerCommand
    {

        // How long the object should shake for.
        public float shakeDuration = 0f;

        // Amplitude of the shake. A larger value shakes the camera harder.
        public float shakeAmount = 0.7f;
        public float decreaseFactor = 1.0f;

        private Vector3 originalPos;
        private GameObject camObject;

        public void Start()
        {
            shakeAmount = GetParameterAsFloat(0);
            decreaseFactor = GetParameterAsFloat(1);
            shakeDuration = GetParameterAsFloat(2);
            camObject = Sequencer.SequencerCamera.gameObject;
            originalPos = camObject.transform.localPosition;
        }

        void Update()
        {
            if (shakeDuration > 0)
            {
                camObject.transform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
                shakeDuration -= Time.deltaTime * decreaseFactor;
            }
            else
            {
                Stop();
            }
        }

        void OnDestroy()
        {
            camObject.transform.localPosition = originalPos;
        }
    }
}
OneManOnMars
Posts: 105
Joined: Tue Apr 05, 2016 9:37 am

Re: Shake camera sequence command

Post by OneManOnMars »

hi Tony,

this is absolutly amazing! Thank you very much for your help. It works perfectly!
I hope other user will find this helpful as well.

Anyway you helped me a lot! Thank you!!!
User avatar
Tony Li
Posts: 21070
Joined: Thu Jul 18, 2013 1:27 pm

Re: Shake camera sequence command

Post by Tony Li »

Happy to help!
Post Reply