Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rewinding animations when player respawns to start of level again.

Discussion in 'Scripting' started by cristo, Dec 9, 2014.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    I'm trying to reset animations on lifts and doors so the player can trigger them again when going though a level again, after repawning to the beginning.

    Is there a way to reset the animation to be triggered again? I tried putting a rewind animation line after the player triggers their respawning.
    I also tried to see whether I could write an animation rewind into the code on the Door gameobject. The second code's pretty shockingly bad, but hopefully someone can see what I'm trying to do....


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Setback : MonoBehaviour {
    5.  
    6.     string TheCollider;
    7.  
    8.     public Transform SpawnPoint, Player, Enemy;
    9.     public GameObject Door;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         SpawnPoint = GameObject.Find ("SpawnPoint").transform;
    14.         Player = GameObject.Find ("Player").transform;
    15.         }
    16.     void OnTriggerEnter (Collider other) {
    17.                 TheCollider = other.tag;
    18.                 if (TheCollider == "Player") {
    19.                 Player.transform.position = SpawnPoint.transform.position;
    20.                 Door.animation.Rewind( "Door open" );
    21.                     }
    22.                 }
    23.              
    24.             }
    25.          
    26.            
    Code (CSharp):
    1.  
    2. public class DoorScript : MonoBehaviour
    3.     {
    4.         GameObject Door;
    5.     public Transform Player;
    6.      
    7.         void Start ()
    8.         {
    9.             Door = GameObject.Find ( "Door" );
    10.         }
    11.      
    12.         public void OnTriggerEnter ( Collider other )
    13.         {
    14.             if (other.tag == "Player") {
    15.                         Door.animation.Play ("door open");
    16.                 }
    17.         }
    18.         void Update (Collider other)
    19.     {
    20.         if (Player.transform.position = SpawnPoint.transform.position) {
    21.         Door.animation.Rewind( "Door open" );
    22.  
    23.     }
    24.     }
    25.  
    26.  
     
    Last edited: Dec 9, 2014
  2. Mystique

    Mystique

    Joined:
    Nov 19, 2014
    Posts:
    40
    This wont work
    Code (CSharp):
    1. if (Player.transform.position = SpawnPoint.transform.position) {
    2.         Door.animation.Rewind( "Door open" );
    3.  
    first, you need to put == , = is for setting equations, == is for checking equations

    second, even if you use == there's a strong chance it wont work because of the way Vectors are rendered.
    They are so precise that the positions will probably never be on the same point.
    Therefore you will probably have to use Vector3.Distance and make it something like this
    Code (CSharp):
    1. Transform SpawnArea; // attach spawn here
    2. Transform Player; // attatch player here
    3.  
    4. void Update () {
    5. var distanceFromSpawn = Vector3.Distance(Player.position, SpawnArea.position); // this will be different in C#
    6.  
    7. if (distanceFromSpawn < 1)
    8. {
    9. print ("player is near the spawn");
    10. }
    11. }
    sorry I forgot how to write C#, line 5 will be different layout
     
    Last edited: Dec 9, 2014
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks for the insight, I'll experiment with that. :)
     
  4. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265

    Hi Mystique, I tried experimenting with your idea in this lift script, trying to get the lift to go back into the down position when the player respawns at the start of the level.

    I don't know how far off it is. Maybe alot.o_O



    Code (CSharp):
    1.  
    2.         public class LiftScript : MonoBehaviour {
    3.         private bool  pressedButton = false;
    4.         private bool  isElevatorUp = false;
    5.         Transform SpawnPoint;
    6.         Transform Player;
    7.  
    8.         GameObject target;
    9.        
    10.         void  OnMouseOver (){
    11.             pressedButton = true;
    12.         }
    13.         void  OnMouseExit (){
    14.             pressedButton = false;
    15.         }
    16.         void  OnMouseDown (){
    17.                 if (isElevatorUp == false) {
    18.                         target = GameObject.Find ("Elevator");
    19.                         target.animation.Play ("UP");
    20.                         isElevatorUp = true;
    21.                 } else {
    22.                         target = GameObject.Find ("Elevator");
    23.                         target.animation.Play ("Down");
    24.                         isElevatorUp = false;
    25.                 }
    26.  
    27.                 }
    28.  
    29.     void Update () {
    30.         {
    31.             SpawnPoint = GameObject.Find ("SpawnPoint").transform;
    32.             Player = GameObject.Find ("Player").transform;
    33.             distanceFromSpawn = Vector3.Distance (Player.position, SpawnPoint.position);
    34.             (distanceFromSpawn < 1);
    35.        
    36.         {
    37.  
    38.             target = GameObject.Find ("Elevator");
    39.             target.animation.Play ("Down");
    40.             isElevatorUp = false;
    41.        
    42.     }
    43.         }
    44. }
     
  5. Mystique

    Mystique

    Joined:
    Nov 19, 2014
    Posts:
    40
    • (PlayersDistanceFromSpawn < 1) should be an if statement.
    Also change that variable name to PlayersDistanceFromSpawn, you can change it later but it will be easier to read for now.

    Code (CSharp):
    1. //start of if statement
    2. if (PlayersDistanceFromSpawn < 1) // if the players distance from spawn is less than 1
    3. {
    4.  
    5.     // insert what you want to happen
    6.  
    7. }
    8. // end of if statement
    9.  
    Code (CSharp):
    1.  void Update () {
    2.  
    3.             SpawnPoint = GameObject.Find ("SpawnPoint").transform;
    4.             Player = GameObject.Find ("Player").transform;
    5.             distanceFromSpawn = Vector3.Distance (Player.position, SpawnPoint.position);
    6.    
    7. if (distanceFromSpawn < 1)
    8.  
    9.         {
    10.             target = GameObject.Find ("Elevator");
    11.             target.animation.Play ("Down");
    12.             isElevatorUp = false;
    13.  
    14.          }
    15. }
    also you might wanna use
    Code (CSharp):
    1. if (PlayersDistanceFromSpawn <=1)
    its a good safeguard to to use a greater/less than sign with an equal sign because sometimes the vector is extremely near 1 and bugs out and doesn't differentiate 1 and less than 1. Because of the decimal numbers being calculated vs framerate it skips a few numbers I think.

    However it will depend on what your doing, for now just using a less than sign should suffice.
     
    Last edited: Dec 9, 2014
  6. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265

    Hey Mystique, I want to thank you so much for your help. I'm newish to coding. I got the sucker working. I had to declare a float for PlayerDistanceFromSpawn. I suppose that's obvious to a good programmer, but it wasn't obvious to me. It felt like a rush realising it's what it needed to work.
    Many thanks again!