Slow Motion Effect on Trigger
I am so close to figuring this out! I am trying to make a slow motion powerup using Time.timeScale, but I am running into several issues. I have watched many tutorials and searched unityanswers for a solution, but almost all the answers discuss initializing the effect on mouseclick down.
Issues:
The Player is unable to collide with the object even though both objects have 2D colliders (trigger is active on powerup and NOT Player). Therefore the effect never occurs. I also want to destroy the powerup when it is picked up.
Here is my code:
Code1:
using UnityEngine;
using System.Collections;
public class PowerUpManager : MonoBehaviour {
private bool doublePoints;
private bool slowMo;
private bool fastMo;
private bool powerUpActive;
private float powerUpLengthCounter;
private ScoreManager theScoreManager;
private float score;
private float slowDown = Time.timeScale ;
// Use this for initialization
void Start () {
theScoreManager = FindObjectOfType <ScoreManager > ();
}
// Update is called once per frame
void Update () {
if (powerUpActive) {
powerUpLengthCounter -= Time.deltaTime;
if (slowMo) {
slowDown = 0.5f;
}
}
if (powerUpLengthCounter <= 0) {
slowDown = 1f;
powerUpActive = false;
}
}
public void ActivatePowerUpSlowMotion(bool slow, float time, float effect){
slowMo = slow;
powerUpLengthCounter = time ;
slowDown = effect;
powerUpActive = true;
}
}
Code 2:
using UnityEngine;
using System.Collections;
public class PowerUp : MonoBehaviour {
public bool doublePoints;
public bool slow;
public bool fast;
public float speed=Time .timeScale ;
public float powerUpLength;
private PowerUpManager thePowerUpManager;
// Use this for initialization
void Start () {
thePowerUpManager = FindObjectOfType <PowerUpManager > ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Player") {
thePowerUpManager.ActivatePowerUpSlowMotion (slow, powerUpLength,speed );
}
gameObject.SetActive (false);
}
}
Your help is appreciated!
What have you tried already?
Did you check if the player object has the "Player" tag assigned? Does the ActivatePowerUpSlowmotion
get triggered? Add a print("x"); in the OnTriggerEntered function to check that.
By the way, you are never changing the timescale, only the value of slowDown.
So even if everything works, and slowDown is set to 0.5f, it doesn't do anything.
Answering the questions I asked.
And also adding timeScale = SlowDown into the Update function of Code 1, so it get's changed to what it should be.
Thanks for getting back to me. The objects are now triggering and the desired effect occurs, but the powerUpLength is not working. When one of the effects is triggered it does not stop. Also I want to keep the player animation speed normal. I have done some research and was trying to use Time.unscaledDeltaTime, but have been unable to make it work.
Here's what I've tried:
using UnityEngine;
using System.Collections;
public class PowerUp$$anonymous$$anager : $$anonymous$$onoBehaviour {
private bool doublePoints;
private bool slow$$anonymous$$o;
private bool fast$$anonymous$$o;
private bool powerUpActive;
private float powerUpLengthCounter;
private Score$$anonymous$$anager theScore$$anonymous$$anager;
private float score;
private float slowDown = Time.timeScale ;
//private float slowDown = Time.unscaledDeltaTime ;
// Use this for initialization
void Start () {
theScore$$anonymous$$anager = FindObjectOfType <Score$$anonymous$$anager > ();
}
// Update is called once per frame
void Update () {
if (powerUpActive) {
powerUpLengthCounter -= Time.unscaledDeltaTime;
if (slow$$anonymous$$o) {
//Time.unscaledDeltaTime = slowDown;
Time.timeScale = slowDown;
slowDown = 0.5f;
}
}
if (powerUpLengthCounter <= 0) {
//Time.unscaledDeltaTime = slowDown;
Time.timeScale = slowDown;
slowDown = 1f;
powerUpActive = false;
}
}
public void ActivatePowerUpSlow$$anonymous$$otion(bool slow, float time, float effect){
slow$$anonymous$$o = slow;
powerUpLengthCounter = time ;
slowDown = effect;
powerUpActive = true;
}
}
Your answer
Follow this Question
Related Questions
Slow Motion PowerUp 0 Answers
my counter wont change from 0 help 1 Answer
Need help with bone animation and trigger code. 0 Answers
OnTriggerEvent doesn't work 1 Answer