Search Unity

how to slow down effect

Discussion in 'Scripting' started by kingcharizard, Oct 31, 2012.

  1. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    I'm kinda stuck, its seems very simple and I always get caught on simple stuff when its late but i need to finish this on a deadline so I'm asking for help..

    What i want to do is slow down the vertical movement so the up and down motion isn't so fast on the object. Any suggestions on how I could do this.. here is the simple script i wrote

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BoxHit : MonoBehaviour {
    5.     public PackedSprite boxSprite;
    6.     private Vector3 box;
    7.     private bool isHit = false;
    8.     // Use this for initialization
    9.     void Start () {
    10.         box = transform.position;
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         transform.position = box;
    16.         if(isHit){this.collider.isTrigger = false;}
    17.     }
    18.    
    19.     void OnTriggerEnter(Collider other){
    20.         box.y += 0.3f;
    21.         Debug.Log("Hit");  
    22.     }
    23.     void OnTriggerExit(Collider other){
    24.         box.y -= 0.3f;
    25.         isHit = true;
    26.     }
    27. }
     
    Last edited: Oct 31, 2012
  2. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    Hi, Firstly don't write your script without Time.deltaTime. It helps you to cover Frame Rate Independent Game.
    Below is just an Idea of working this. Make modification as you required.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BoxHit : MonoBehaviour {
    5.  
    6.     public PackedSprite boxSprite;
    7.  
    8.     public float currBoxSpeed = 30.0f;
    9.     public float normalBoxSpeed = 30.0f;
    10.     private float minBoxSpeed = 15.0f;
    11.  
    12.     private bool isHit = false;
    13.  
    14.     // Use this for initialization
    15.  
    16.     void Start () {
    17.          currBoxSpeed  = normalBoxSpeed ;
    18.     }
    19.  
    20.    
    21.  
    22.     // Update is called once per frame
    23.  
    24.     void Update () {
    25.               transform.Translate(Vector3.Up * boxSpeed * Time.deltaTime)
    26.     }
    27.  
    28.    
    29.  
    30.     void OnTriggerEnter(Collider other){
    31.          currBoxSpeed  = minBoxSpeed ; //slow Down here.
    32.     }
    33.  
    34.     void OnTriggerExit(Collider other){
    35.          currBoxSpeed  = maxBoxSpeed ;
    36.     }
    37.  
    38. }
     
  3. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    Whats this? I know all about Time.deltaTime. this is not what I was looking for. I'm going to research a new way to achieve what I want in the morning thanks anyways
     
  4. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Looks to me like he gave you exactly what you were after, which makes your response more than a little rude. Best of luck...
     
  5. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    kingcharizard, I wrote what you want. Game Object Speed depends on Box Speed, change it to make it slow or fast. For Up Down, i just added code for UP, just make it for DOWN by yourself.
     
  6. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    How are you gonna tell me what works for my game? This is not my first project or my first script i'm not a beginner. That response wasn't rude it was strait to the point, he posted a script I didn't need so I told him so... you posting nothing relevant to the topic is rude..

    your code moves a object up at a certain speed, what my code does is moves a certain object up on the y axis a fixed amount and then moves it back down, I'll explain.

    When the player collides with the boxObject the object moves up on the y axis
    Code (csharp):
    1.  void OnTriggerEnter(Collider other){
    2.  
    3.         box.y += 0.3f;
    4.  
    5.         Debug.Log("Hit");  
    6.  
    7.     }
    then when the player leaves the collider i move the box back into place using this code
    Code (csharp):
    1.  void OnTriggerEnter(Collider other){
    2.  
    3.         box.y -= 0.3f;
    4.         isHit = true;
    5.      
    6.  
    7.     }
    I then updated the movement every frame with this code
    Code (csharp):
    1.  // Update is called once per frame
    2.  
    3.     void Update () {
    4.  
    5.         transform.position = box;
    6.  
    7.         if(isHit){this.collider.isTrigger = false;}
    8.  
    9.     }
    10.  
    this works fine because it doesn't move the box unless im in contact with the collider.

    Whats its doing is moving up really fast and back down in place really fact which isn't what I want. I want the effect to be a bit slower. I don't want the box to move up at a certain speed. I need it to move up and back into place at a certain speed.

    I've tried to adjust the speed using variables and Time.deltaTime but when I put code like this in update
    Code (csharp):
    1.     transform.position = (box * Time.deltaTime);
    it just raises the box even when im not in the trigger.
    when i try to add speed to the box.y variable is adds to the effect making it bigger or smaller so thats a no go either.. like I said I'm going to figure out a better way to achieve my desired result...
     
    Last edited: Oct 31, 2012
  7. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    As you wrote that you are not a beginner. Then what does it mean to write: transform.position = box;

    And Regarding my code lines transform.position = (box * Time.deltaTime); .. You should add these lines in a flag Check(enable on Trigger Enter with Player Disable on Trigger Exit)
    If you want to move box by a particular unit(lets say 3units Up), in this case you should use a basic concept of store initial position to a initPosition, in Update() make a difference check whether you difference is greater than maxVar(Which is 3Units) perform operations on this basis.
     
  8. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    Code (csharp):
    1. transform.position = box;
    this takes the box's updated position and puts it in transform.position
    as you should know the value on the right gets added or becomes the value on the left.

    I hard coded everything till i get it right. I don't use the inspector to change my values and since the script wasn't long i didn't mind changing both values.

    this code
    Code (csharp):
    1. transform.position = (box * Time.deltaTime);
    was my idea i used it to try to slow down the effect but it just incremented it constantly. I've tried many different ways before asking for help if this was simple i should have figured it out

    I dont understand what your getting at here
    but from reading that again I dont think you understand what I'm trying to achieve so here is a webplayer demo hit the question mark box

    http://dl.dropbox.com/u/36346467/WebPlayer.html

    see how fast the box goes up and down. I want that effect to slow down
     
    Last edited: Oct 31, 2012
  9. AMDAndrew

    AMDAndrew

    Joined:
    Aug 10, 2012
    Posts:
    90
    Last edited: Oct 31, 2012
  10. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    yeah i was looking into the animation idea
     
  11. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    The web player isn't working for me, but I think I understand what you're after. You basically want an automatic opening and closing garage door. Is there a reason why you can't do this this with a Unity Animation? It seems simple to me.

    But it is also easy enough to code, lots of ways you can do it. Here's a basic state machine approach...

    Code (csharp):
    1.  
    2.     bool hitEnter = false;
    3.     bool hitExit = false;
    4.     bool liftUp = false;
    5.     bool liftDown = false;
    6.     float liftDuration = 0.5f; // seconds
    7.     Vector3 boxLift = new Vector3(0, -0.3f. 0);
    8.     Vector3 boxStart;
    9.     float liftStartTime;
    10.  
    11.     void Start () {
    12.         boxStart= transform.position;
    13.     }
    14.  
    15.     void Update () {
    16.         if (hitEnter) {
    17.             liftStartTime = Time.time;
    18.             liftUp = true;
    19.             hitEnter = false;
    20.         }
    21.  
    22.         if (liftUp) {
    23.             float dt = (Time.time - liftStartTime) / liftDuration;
    24.             if (dt >= 1) {
    25.                 dt = 1;
    26.                 liftUp = false;
    27.                 liftDown = true;
    28.                 liftStartTime = Time.time;
    29.             }
    30.             transform.position = Vector3.Lerp(boxStart, boxStart+boxLift, dt);
    31.         }
    32.        
    33.         if (liftDown) {
    34.             float dt = (Time.time - liftStartTime) / liftDuration;
    35.             if (dt >= 1) {
    36.                 dt = 1;
    37.                 liftDown = false;
    38.             }
    39.             transform.position = Vector3.Lerp(boxStart+boxLift, boxStart, dt);
    40.         }
    41.  
    42.         if (hitExit)
    43.         {
    44.             // reset state
    45.             liftUp = false;
    46.             liftDown = false;
    47.             hitExit = false;
    48.             transform.position = boxStart;
    49.         }
    50.     }
    51.    
    52.     void OnTriggerEnter(Collider other) {
    53.         hitEnter = true;
    54.     }
    55.  
    56.     void OnTriggerExit(Collider other) {
    57.         hitExit = true;
    58.     }
    59.  
    This code is untested.

    I should also mention that this program assumes the actor is passing through the collision volume and takes enough time to allow the door, or whatever it is, to lift up and back down. This is an assumption on my part. You haven't explained your application in enough detail to know exactly what you want to do. And, unfortunately, I still can't get the web player to play nice in Chrome, so I can't look at your project.... I don't think this is your fault though, I think something is broken on my end.
     
    Last edited: Oct 31, 2012
  12. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    You asked a question and got an answer. It wasn't the answer you were looking for, and where you _might_ have considered thanking them for their time, effort, input... and then explained that you were looking for a different solution... you instead responded like a spoiled child. The fact that you can't see how incredibly rude your reply was simply tells me you don't even realize when you're being rude. Whether this is your first rodeo or not, I suggest you get your Pikachu underoos unknotted. Best of luck with that and your code. 'Nuff said.
     
    Last edited: Oct 31, 2012
  13. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    please stay out of my topic, but if you intend and insist on wasting my time then tell me how i was rude?
    I told them directly their code"effort" didn't get what i wanted and proceeded to tell them i would solve the problem on my own. Spoiled child? really? far from it infact i am poor, and im proud of where I come from because when i succeed It will be because I worked my ass off to achieve what i have. which is the case now.

    But if you insist i was being rude please tell me how should have answered the reply...
     
    Last edited: Oct 31, 2012
  14. AMDAndrew

    AMDAndrew

    Joined:
    Aug 10, 2012
    Posts:
    90
    ohoho Easy here ultrafree people of USA. Krougeu you are more likely a spoiled child, you must explain him what happens around here instead of starting to swear because you think of yourself as an "grown-up"
     
  15. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    oh btw everything you said i should have done I'm pretty sure I actually did

    End of discussion! if I do not get the desired result that i want i will post again..
     
    Last edited: Oct 31, 2012
  16. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
     
  17. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    I'm no expert, but what I would do to slow down time I just do this :

    Code (csharp):
    1.  
    2. "Thing to slow down" = Time.deltaTime / "amount I want to slow down";
    3.  
     
  18. Gorlog

    Gorlog

    Joined:
    Jan 20, 2011
    Posts:
    201
  19. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    I tried that it didn't quite work right. I already got it thanks anyways.

    Code (csharp):
    1. "Thing to slow down" = Time.deltaTime / "amount I want to slow down";
    I've tried that it either adds/subtracts to the effect deppending on the result of the math.. I figured it out. I no longer need help, but thanks anyways
     
  20. sushanta1991

    sushanta1991

    Joined:
    Apr 3, 2011
    Posts:
    305
    Hi Guys, I think this is a case of Misunderstanding each other :)
    And @kingcharizard I think this is what you want.

    Code (csharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.      
    5.     public class BoxHit : MonoBehaviour {
    6.         //public PackedSprite boxSprite;
    7.         private Vector3 box;
    8.         private bool isHit = false;
    9.         private bool SlowDown = false;
    10.         // Use this for initialization
    11.         void Start () {
    12.             box = transform.position;
    13.         }
    14.        
    15.         // Update is called once per frame
    16.         void Update () {
    17.         print(SlowDown);
    18.             transform.position = box;
    19.             if(isHit){this.collider.isTrigger = false;}
    20.             if(SlowDown){StartCoroutine(Wait());SlowDown = false;}
    21.        
    22.         }
    23.        
    24.         void OnTriggerEnter(Collider other){
    25.             box.y += 0.3f;
    26.             Debug.Log("Hit");  
    27.         }
    28.         void OnTriggerExit(Collider other){
    29.             SlowDown = true;
    30.         }
    31.         IEnumerator Wait()
    32.         {
    33.             yield return new WaitForSeconds(1);
    34.             box.y -= 0.3f;
    35.             isHit = true;
    36.         }
    37.     }
    38.  
    I tried the code its working.