Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

collect coins

Discussion in 'Getting Started' started by joytdecastro, Feb 22, 2015.

  1. joytdecastro

    joytdecastro

    Joined:
    Feb 15, 2015
    Posts:
    21
    guys can you help me? im doing a similar game to temple run, and i need to have a power up that collecting coins. when the character get the power up, the coins will move towards the character, and it has a timer, please help me, thanks in advance
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Instead of just giving you code that would work, I'd like to try to teach you to think like a programmer.

    You need to have the coin itself do something, right? That would suggest you need to add a script to the coin object.

    You need to have it move towards the player? So the script should hold a reference to the player object.

    You only want it to do that when a certain condition is true? You got it... You'll need a variable stored somewhere that the coin can check against.

    Break things down one by one. If you don't know how to make that single task happen, Google it. I'm sure someone's asked it before.
     
    CodeMonke234 likes this.
  3. joytdecastro

    joytdecastro

    Joined:
    Feb 15, 2015
    Posts:
    21
    i already had it, but, now my problem was, my coin will respawn everytime the player pass through it. so when the coin will respawn, the coin will also move towards the player, here is my code for the coin.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var coinEffect : Transform;
    4. var coinValue = 1;
    5. var timer = 0.0;
    6.  
    7. function OnTriggerEnter (info : Collider)
    8. {
    9. if (info.tag == "Player"){
    10.     GameMaster.currentScore += coinValue;
    11.     var effect = Instantiate(coinEffect, transform.position, transform.rotation);
    12.     StartCoroutine("Respawn");
    13.     }
    14. }
    15.  
    16. function Respawn()
    17. {
    18.     yield WaitForSeconds(1);
    19.     gameObject.GetComponent(MeshRenderer).renderer.enabled = true;
    20.     gameObject.GetComponent(BoxCollider).collider.enabled = true;
    21. }
    and for the player script...

    Code (JavaScript):
    1. #pragma strict
    2. var jumpctr : int = 0; //counting the anim for jumping
    3. var fSpeed : float = 3; //i defined variable speed forward so that it can be adjust thru time
    4. var nextPosition : Vector3; //Vector3 for the next position
    5. var nextRotation : Quaternion; //same here VEctor3 for the next rotation
    6. var isTurnTrigger : boolean = false;
    7. var isJump : boolean = false;
    8. var isTurnLR : String = "";
    9. var turnctr : int = 0;
    10. var ghostTime : float;
    11. var isGhost : boolean;
    12. var isEndTrigger : boolean = false;
    13.  
    14. function Update () {
    15.     AutoForward();
    16.     Controllers();
    17.     checkUPS();
    18.    
    19.  
    20. }
    21.  
    22. function AutoForward(){
    23.     fSpeed *= 1.0001; //speed up
    24.     nextPosition = transform.forward * fSpeed ;
    25.     rigidbody.MovePosition(rigidbody.position + nextPosition * Time.deltaTime);
    26. }
    27.  
    28. function Controllers(){
    29.    
    30.     if(!isTurnLR){ //if isturnLR empty?
    31.         if(isTurnTrigger){ //if isTurnTrigger true
    32.             if(Input.GetKeyDown(KeyCode.A)){
    33.                 isTurnTrigger = false;
    34.                 isTurnLR = "LEFT";
    35.             } else if(Input.GetKeyDown(KeyCode.D)){
    36.                 isTurnTrigger = false;
    37.                 isTurnLR = "RIGHT";
    38.             }
    39.         } else{
    40.             if(Input.GetKey(KeyCode.A)){
    41.                 nextPosition = transform.right * -2 ;
    42.                 rigidbody.MovePosition(rigidbody.position + nextPosition * Time.deltaTime);
    43.             } else if(Input.GetKey(KeyCode.D)){
    44.                 nextPosition = transform.right * 2 ;
    45.                 rigidbody.MovePosition(rigidbody.position + nextPosition * Time.deltaTime);
    46.             }
    47.         }
    48.     } else{
    49.         rotateAnimPlayer();
    50.     }
    51.     if(isJump){
    52.         isJumping();
    53.     }else{
    54.         if(Input.GetKeyDown(KeyCode.Space)){
    55.             isJump = true;
    56.         }
    57.     }
    58. }
    59. function checkUPS(){
    60.     if(isGhost){
    61.         if(Time.time - ghostTime > 5){
    62.             gameObject.renderer.material.color = Color.white;
    63.             isGhost = false;
    64.         }
    65.     }
    66. }
    67. function rotateAnimPlayer(){
    68.     if(turnctr < 9){
    69.         if(isTurnLR=="LEFT"){
    70.             nextRotation = Quaternion.Euler (0, -10, 0);
    71.             rigidbody.MoveRotation(rigidbody.rotation * nextRotation);
    72.         } else{
    73.             nextRotation = Quaternion.Euler (0, 10, 0);
    74.             rigidbody.MoveRotation(rigidbody.rotation * nextRotation);
    75.         }
    76.         turnctr += 1;
    77.     } else{
    78.         turnctr = 0;
    79.         isTurnLR = "";
    80.     }
    81. }
    82.  
    83. function isJumping(){
    84.     if(jumpctr < 6){
    85.         nextPosition = new Vector3(0, 4, 0);
    86.         rigidbody.MovePosition(rigidbody.position + nextPosition * Time.deltaTime);
    87.         jumpctr += 1;
    88.     } else{
    89.         isJump = false;
    90.         jumpctr = 0;
    91.     }
    92. }
    93.  
    94.  
    95. function OnTriggerEnter (other : Collider){
    96.  
    97.     if(other.gameObject.name == "EndTirgger"){
    98.         Time.timeScale = 0;
    99.         Debug.Log("hit");
    100.     }  
    101.  
    102.     if(other.name == "TurnTrigger"){
    103.         isTurnTrigger = true;
    104.     } else if(other.name == "Coin"){
    105.     //Destroy(other.gameObject);
    106.     other.gameObject.GetComponent(MeshRenderer).renderer.enabled = false;
    107.     other.gameObject.GetComponent(BoxCollider).collider.enabled =false;
    108.     isGhost = true;
    109.     ghostTime = Time.time;
    110.     gameObject.renderer.material.color = Color.cyan;
    111.     }
    112.    
    113. }
    114.  
    115. function OnCollisionEnter (other : Collision){
    116.     if(other.gameObject.name == "Wall") {
    117.         if (GameMaster.currentScore > PlayerPrefs.GetInt("txtscore")){
    118.         PlayerPrefs.SetInt("txtscore", GameMaster.currentScore);
    119.         }
    120.         Application.LoadLevel("Main Menu");
    121.     }
    122.     else if(other.gameObject.name == "boat1") {
    123.     if (GameMaster.currentScore > PlayerPrefs.GetInt("txtscore")){
    124.         PlayerPrefs.SetInt("txtscore", GameMaster.currentScore);
    125.         }
    126.         Application.LoadLevel("Main Menu");
    127.     }
    128.     else if(other.gameObject.name == "boat2") {
    129.     if (GameMaster.currentScore > PlayerPrefs.GetInt("txtscore")){
    130.         PlayerPrefs.SetInt("txtscore", GameMaster.currentScore);
    131.         }
    132.         Application.LoadLevel("Main Menu");
    133.     }
    134.     else if(other.gameObject.name == "boat3") {
    135.     if (GameMaster.currentScore > PlayerPrefs.GetInt("txtscore")){
    136.         PlayerPrefs.SetInt("txtscore", GameMaster.currentScore);
    137.         }
    138.         Application.LoadLevel("Main Menu");
    139.     }
    140.     else if(other.gameObject.name == "Rock1") {
    141.     if (GameMaster.currentScore > PlayerPrefs.GetInt("txtscore")){
    142.         PlayerPrefs.SetInt("txtscore", GameMaster.currentScore);
    143.         }
    144.         Application.LoadLevel("Main Menu");
    145.     }
    146.     else if(other.gameObject.name == "Rock2") {
    147.     if (GameMaster.currentScore > PlayerPrefs.GetInt("txtscore")){
    148.         PlayerPrefs.SetInt("txtscore", GameMaster.currentScore);
    149.         }
    150.         Application.LoadLevel("Main Menu");
    151.        
    152.        
    153.     }
    154.    
    155. }
    156.  
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I see a lot of string comparisons. These will ultimately make your scripts unwieldy and difficult to maintain.
     
  5. joytdecastro

    joytdecastro

    Joined:
    Feb 15, 2015
    Posts:
    21
    ahhmm, well that's all i can do to that, i don't know how to fix that,
     
    Last edited: Feb 27, 2015