• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Minstrel · Oct 16, 2013 at 02:15 AM · scaledebuggingcollectible

Debugging - Scale Collectible

Hi guys,

I've been working on a script that grows / shrinks a rigid body asset (including the player) on trigger. I currently have 2 scripts; "Collectible" for the the trigger to apply the effect and "Effects" which is added to the rigid body collision object and makes the scale change.

I've been working on it a while and hit a maths wall with how to stack these effects smoothly - resizing to normal is fine, but when adding new collectibles the animation jumps back towards normal size. Can anyone see where I've gone wrong? I think I've isolated it to the following section;

 // Start Transition
 if(timer > duration - transitionDuration) {
     if(modifier != prevModifier) {
        prevModifier = modifier * (duration - timer);
        transform.localScale = defaultScale * (1 + prevModifier);
     }
 }

(On stacking, timer is set back to duration and modifier is increased / decreased.)

Also, any tips on making this generic to any variable (speed etc) without too much code repetition? (eg. Is variable reference forwarding possible?)

I have included the rest of my code below and hope other find it useful.

Effects

 using UnityEngine;
 using System.Collections;
 
 public class Effects : MonoBehaviour {
     
     // The types of attribute effects available
     public enum EffectTypes {Scale}
     public EffectTypes effectType = EffectTypes.Scale;
     
     // Modifier controls
     public float modifier = 0.5f;        // The modifier to aim for
     private float prevModifier = 0;        // The staggered modifier previously used in calculations
     public float minModifier = -0.5f;    // Minimum modifier value (avoids gameObject flipping etc)
     public float maxModifier = 1.0f;    // Maximum modifier value (avoids growing endlessly)
     
     // Essential controls
     public bool stackEffects = true;    // Do the effects stack?
     public float stackAmount = 0.25f;    // Percent of new modifier added when stacking
     private float timer = 5;            // Controls the countdown
     
     // Transition durations
     public float duration = 5;            // Length the effect lasts in seconds
     public float transitionDuration = 1;// Length of the start / end transitions
     
     // Type specific stored values
     private Vector3 defaultScale;        
     
     // Preset the stored animations
     void Awake() {
         // Initialise the game object variables
         defaultScale = transform.localScale;
         
         // Ensure the variables are valid
         modifier = Mathf.Clamp(modifier, minModifier,maxModifier);
         stackAmount = Mathf.Clamp (stackAmount, 0.01f, 1);
         transitionDuration = Mathf.Clamp (transitionDuration, 0.01f, duration / 2);
     }
     
     // Apply the effect and control timer
     void Update() {
         if(effectType == EffectTypes.Scale) {
             ApplyScale();
         }
         timer -= Time.deltaTime;
     }
     
     // Apply the effect to the scale variable
     public void ApplyScale() {
         // Start Transition
         if(timer > duration - transitionDuration) {
             if(modifier != prevModifier) {
                 prevModifier = modifier * (duration - timer);
                 transform.localScale = defaultScale * (1 + prevModifier);
             }
         }
         // Inbetween transitions
         else if(timer > transitionDuration ) { }
         // End transition
         else if(timer > 0) {
             transform.localScale = defaultScale + defaultScale * modifier * timer;
         }
         // Destroy script on completion
         else {
             transform.localScale = defaultScale;
             Destroy (this);
         }
     }
     
     // Directly set the modifier to a value
     public void SetModifier(float mod) {
         modifier = Mathf.Clamp(mod, minModifier, maxModifier);
         timer = duration;
     }
     
     // Reset the timer, and if stackable, stack
     public void CombineEffects(float mod) {
         timer = duration;
         
         // If stack effects is enabled, stack by the stack amount
         if(stackEffects) 
             SetModifier(modifier + mod * stackAmount);
     }
 }


Collectible

 using UnityEngine;
 using System.Collections;
 
 public class Collectible : MonoBehaviour {
     
     public Effects.EffectTypes effect;
     public float modifier = 0.5f;
     
     void OnTriggerEnter(Collider col) {
         if(col.gameObject.GetComponent<Rigidbody>() != null) {
             Effects[] eff = col.gameObject.GetComponents<Effects>();
             bool found = false;
             
             // Determine if the effect has already been applied
             if(eff != null) {
                 for(int i = 0; i < eff.Length; i ++){
                     // If the effect is the same, reset the duration and end the loop
                     if(eff[i].effectType == effect){
                         eff[i].CombineEffects(modifier);
                         found = true;
                         i = eff.Length;
                         
                         // Destroy the collectable
                         Destroy(gameObject);
                     }
                 }
             }
             
             // If there is no effect, apply it
             if(!found || eff == null) {
                 Effects newEffect = col.gameObject.AddComponent(typeof(Effects)) as Effects;
                 if(modifier != 0) {
                     newEffect.SetModifier(modifier);
                 }
                 // Destroy the collectable
                 Destroy(gameObject);
             }
         }
     }
 }


Edit: Cleanup to shorten and refine Effects script. *Edit: Figured out clamping to remove flipping problem.

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Minstrel · Oct 16, 2013 at 02:28 AM 0
Share

For ease of testing, I'll also attach my current respawn code. Just set the respawnTime to positive.

 using UnityEngine;
 using System.Collections;
 
 public class SpawnPoint : MonoBehaviour {
     
     public GameObject spawns;        // Object to spawn
     public float respawnTime = -1;    // Default time between spawning
     public float respawnRandom = 2;    // Time variation between spawning
     public float respawnHeight = 1; // The height at which the collectable spawns
     
     private float timer = -1;        // count down timer
     private GameObject currInstance;
     
     void Start () {
         Spawn();
     }
     
     // Update is called once per frame
     void Update () {
         if(respawnTime != -1 && currInstance == null) {
             if(timer > 0) {
                 timer -= Time.deltaTime;
             }
             else {
                 Spawn();
                 timer = Random.Range(respawnTime - respawnRandom/2, respawnTime + respawnRandom/2);
             }
         }
     }
     
     // Instantiate the object
     public void Spawn() {
         if(!currInstance) {
             currInstance = Instantiate(spawns) as GameObject;
             currInstance.transform.parent = transform;
             Vector3 newPosition = transform.position;
             newPosition.y += respawnHeight;
             currInstance.transform.position = newPosition;
         }
     }
 }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't change the size of the terrein 1 Answer

i need help with this camera switcher 3 Answers

How to deal with scale of imported models and scripting 0 Answers

Die on collision doesnt work 2 Answers

Objects to fit on the screen 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges