Search Unity

Help with Starting and stopping a scrolling script

Discussion in '2D' started by Plott, Jul 20, 2014.

  1. Plott

    Plott

    Joined:
    May 12, 2014
    Posts:
    94
    Ok, I got this script to work earlier on, but now that I want to have a 'stop' scrolling function it isn't working correctly. The problem is I want to call on my "Player" from a different gameObject and say if Player is moving in X scroll = true, and if Player isn't moving in X scroll = false.

    OR it can call on an animation trigger, like "Anime fall" or "AnimeDeath"

    I tried to re write this but its not working.

    see below.

    Original script. "False" never gets called because I haven't defined how to turn off IsAlive (maybe the easiest solution, but can't figure it out)

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Linq;
    3. using UnityEngine;
    4.  
    5.  
    6. public class ScrollingScript : MonoBehaviour
    7. {
    8.     public Vector2 speed = new Vector2(2, 2);
    9.     public Vector2 direction = new Vector2(-1, 0);
    10.     public bool isAlive = false;
    11.     public bool isLooping = false;
    12.     private List<Transform> backgroundPart;
    13.    
    14.  
    15.     void Start ()
    16.     {
    17.         if (isLooping)
    18.         {
    19.             backgroundPart = new List<Transform>();
    20.            
    21.             for (int i = 0; i < transform.childCount; i++)
    22.             {
    23.                 Transform child = transform.GetChild(i);
    24.  
    25.                 if (child.renderer != null)
    26.                 {
    27.                     backgroundPart.Add(child);
    28.                 }
    29.             }
    30.  
    31.             backgroundPart = backgroundPart.OrderBy(
    32.                 t => t.position.x
    33.                 ).ToList();
    34.         }
    35.     }
    36.    
    37.    
    38.     void Update()
    39.     {
    40.         if (Input.GetMouseButtonDown (0))
    41.         {
    42.             isAlive = true;
    43.         }  
    44.  
    45.         if (isAlive == true)
    46.         {
    47.             Vector3 movement = new Vector3 (speed.x * direction.x, speed.y * direction.y, 0);  
    48.             movement *= Time.deltaTime;
    49.             transform.Translate (movement);
    50.  
    51.             if (isLooping)
    52.             {
    53.                 Transform firstChild = backgroundPart.FirstOrDefault();
    54.                
    55.                 if (firstChild != null)
    56.                 {
    57.  
    58.                     if (firstChild.position.x < Camera.main.transform.position.x)
    59.                     {
    60.  
    61.                         if (firstChild.renderer.IsVisibleFrom(Camera.main) == false)
    62.                         {
    63.  
    64.                             Transform lastChild = backgroundPart.LastOrDefault();
    65.                             Vector3 lastPosition = lastChild.transform.position;
    66.                             Vector3 lastSize = (lastChild.renderer.bounds.max - lastChild.renderer.bounds.min);
    67.                             firstChild.position = new Vector3(lastPosition.x + lastSize.x, firstChild.position.y, firstChild.position.z);
    68.                             backgroundPart.Remove(firstChild);
    69.                             backgroundPart.Add(firstChild);
    70.                         }
    71.                     }
    72.                 }
    73.             }
    74.         }
    75.  
    76.         if (isAlive == false)
    77.         {
    78.             Vector3 movement = new Vector3 (0, 0, 0);  
    79.             movement *= Time.deltaTime;
    80.             transform.Translate (movement);
    81.         }
    82.  
    83.     }
    84.  
    85. }
    86.  
    Now i wanted to write it something like

    Code (CSharp):
    1.    voidAwake ()
    2. {
    3. animator = GameObject.FindGameObjectWithTag("Player").GetComponent<Animator>();
    4.  
    5. }
    Code (CSharp):
    1. if (animator.SetTrigger ("AnimFall")
    2. {
    3. isAlive = true;
    4. }
    and then

    Code (CSharp):
    1. if (animator.SetTrigger ("AnimDeath")
    2. {
    3. isAlive = false;
    4. }
    OR it could be the same as above but make the x<0 , but I'm not sure how to write that correctly

    instead of

    Code (CSharp):
    1. if (Input.GetMouseButtonDown (0))
    2. {
    3. isAlive = true;
    4. }
    I'm super new and teaching myself c# right now, sorry if i butchered this lol.
     
  2. Punchbag

    Punchbag

    Joined:
    Oct 30, 2012
    Posts:
    48
    Your C# is looking pretty good. I work with people who don't write code this clean.

    Your choice is either:
    • Grab a member reference to the player object using FindObjectOfType in the ScrollableScript's start (preferred solution, as the player doesn't need to know about the scrollable, but the scrollable needs to know about the player) and then set isAlive based on the player's X, or;
    • Grab the Scrollable in the Player object's Start (icky), and set isAlive accordingly (by making it public, [ickier]).
    Then you're pretty much set!

    If this is for a scrolling background, though, you'd probably want to scroll based on camera position rather than player position. This will feel more correct if you ever move the camera independently of the player (and your scrolling is for parallax purposes).