playMaker

Author Topic: Camera Shake via script  (Read 2224 times)

jess84

  • Hero Member
  • *****
  • Posts: 515
Camera Shake via script
« on: June 27, 2014, 08:46:52 AM »
Hello,

So I was looking at the DFTween Camera Shake example;

It sits on the Cube object, and it triggers a tween shake on the Camera object specified by the player.

Code: [Select]
using UnityEngine;
using System.Collections;

using DaikonForge.Tween;

public class TestShakeObject : MonoBehaviour
{

public Transform Camera;

private TweenShake shake;
private TweenBase fallTween;

void Start()
{

shake = TweenShake.Obtain()
.SetStartValue( Camera.position )
.SetDuration( 1f )
.SetShakeMagnitude( 0.25f )
.SetShakeSpeed( 13f )
.SetTimeScaleIndependent( true )
.OnExecute( ( result ) => { Camera.position = new Vector3( result.x, result.y, Camera.position.z ); } );

fallTween = this.TweenPosition()
.SetStartValue( transform.position + Vector3.up * 5f )
.SetEasing( TweenEasingFunctions.EaseInExpo )
.SetDuration( 1f )
.SetDelay( 0.25f )
.SetTimeScaleIndependent( true )
.OnCompleted( ( sender ) => { shake.Play(); } )
.Play();

}

void OnGUI()
{
GUILayout.Label( " Press SPACE to restart " );
}

void Update()
{

if( Input.GetKeyDown( KeyCode.LeftArrow ) )
{
Debug.Log( "Time scale: SLOW" );
Time.timeScale = 0.15f;
}
else if( Input.GetKeyDown( KeyCode.RightArrow ) )
{
Debug.Log( "Time scale: NORMAL" );
Time.timeScale = 1f;
}

if( Input.GetKeyDown( KeyCode.Space ) )
{
fallTween.Stop().Rewind().Play();
}

}

}

How should this be amended so that I can put the script on my camera, and just enable/disable it as needed via a PlayMaker Enable Behaviour action?

jess84

  • Hero Member
  • *****
  • Posts: 515
Re: Camera Shake via script
« Reply #1 on: June 28, 2014, 04:47:26 PM »
bump

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4002
  • Official Playmaker Support
    • LinkedIn
Re: Camera Shake via script
« Reply #2 on: June 28, 2014, 07:19:55 PM »
Taking a quick look, you should put this inside OnEnable:

fallTween.Stop().Rewind().Play();

And this inside OnDisable:

fallTween.Stop();

jess84

  • Hero Member
  • *****
  • Posts: 515
Re: Camera Shake via script
« Reply #3 on: June 29, 2014, 07:42:18 AM »
Thanks, but it doesn't seem to work for me.

I guess I'll just wait for playmaker actions if they're done in the future.

Cheers anyway.