Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

my bananas need force to move

Discussion in '2D' started by Anoshan56, Dec 18, 2014.

  1. Anoshan56

    Anoshan56

    Joined:
    Dec 18, 2014
    Posts:
    13
    i want bananas spawn at the top right corner and drop off one by one due to gravity and move left slowly.i'm a beginner new to coding and untiy.So please help
     
  2. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    That's the best thread title of 2014. Congrats. :D
     
  3. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
  4. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    research mesh colliders and rigid bodies.
    May want to rig to a bone and animate moving to the left slowly too.
     
  5. Anoshan56

    Anoshan56

    Joined:
    Dec 18, 2014
    Posts:
    13
    what i want is ,the code to make bananas drop one by one and move left slowly.
     
  6. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    What have you done so far to attempt to create the desired effect on your own?
     
  7. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try this :D
     

    Attached Files:

  8. Anoshan56

    Anoshan56

    Joined:
    Dec 18, 2014
    Posts:
    13
    thanks
     
  9. Anoshan56

    Anoshan56

    Joined:
    Dec 18, 2014
    Posts:
    13
    thanks very much,but can i know how to make that banana move left once its on the ground
     
  10. Anoshan56

    Anoshan56

    Joined:
    Dec 18, 2014
    Posts:
    13
    + can i know how to give a constant force to the banana
    + destroy bananas that have fallen from the ground(floor)
     
  11. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    there more ways to move GameObject. If it has Rigidbody2D, then you can use (in c#)
    rigidbody2D.velocity = new Vector2(x, y); for constant speed. For bananas move to left it would be.

    public float Speed;
    void Start () { rigidbody2D.velocity = new Vector2(-Speed, rigidbody2D.velocity.y);}

    If you dont need physic, so you can just use transform.translate. It would be something like:

    public vector2 move;
    void Update () { transform.Translate (Move * Time.deltaTime);}

    Time.deltaTime for smooth moving.


    for destroy use Destroy (gameObject);

    you can destroy it, if it is out of screen :
    void OnBecameInvisible () {Destroy (gameObject); }

    or destroy after some time:

    void Start () {Invoke("NoBananas", 5);}
    void NoBananas () {Destroy (gameObject); }

    but as said, everytime you have more ways to achieve the goal.
     
  12. Anoshan56

    Anoshan56

    Joined:
    Dec 18, 2014
    Posts:
    13
    thank u very much,:)
    but i'm having difficulty getting my groundcheck to work.in my scene bananas drop one by one from the sky and fall on the floor due to gravity,after it has fallen on the floor it should move left at a certain speed. .
     
  13. Anoshan56

    Anoshan56

    Joined:
    Dec 18, 2014
    Posts:
    13
    thanks i was able to do that using animations.but as my first banana falls off the ground more bananas aren't spawned.How do i fix it.
     
  14. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    you need spawn point for bananas. Make prefab of GameObject Banana (sprite, collider2D, rigidbody2d usw.). Add empty GameObject to scene, where bananas should be spawned. Make script for bananaspawn, with code line:

    void update () {Instantiate (BananaPrefabName, transform.position, transform.rotation);}

    I cannot say, what is wrong, if i dont see script. Maybe you have destroyed bananaspawner. Show your scripts here.
     
  15. Anoshan56

    Anoshan56

    Joined:
    Dec 18, 2014
    Posts:
    13
    ok i've done somewhat.now i want to destroy the banana instantiates that have moved away from the camera/become invisible.But this process should be repeated so i don't want the original banana be destroyed only the clones.
     
  16. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    i think, you dont have banaspawn in the scene. There should not be some original bananas, only clones from spawner. And the clones should destroy it self.

    try to run asset in my first post.
     
  17. Anoshan56

    Anoshan56

    Joined:
    Dec 18, 2014
    Posts:
    13
    but is there a way to destroy my original banana clones without destroying the first original banana(not clone)
     
  18. Anoshan56

    Anoshan56

    Joined:
    Dec 18, 2014
    Posts:
    13
    hi,well i've got another problem.this is my code for both the score and highscore(is shown in the game over scene)
    the score works fine but this is my problem "Assets/score.cs(12,17): error CS0029: Cannot implicitly convert type `bool' to `float'" and here's my code

    using UnityEngine;
    using System.Collections;

    public class score : MonoBehaviour {

    float playerScore = 0;
    float highscore = 0;
    float highestscore = 0;

    void Update () {
    playerScore += Time.deltaTime;
    highestscore = playerScore > highscore;
    }
    public void IncreaseScore(int amount)
    {
    playerScore += amount;

    }

    void OnDisable()
    {
    PlayerPrefs.SetInt ("Score", (int)playerScore * 3 -3);
    PlayerPrefs.SetInt ("Highscore", (int)highestscore);
    }
    void OnGUI()
    {
    GUI.Label (new Rect (10, 10, 100, 30), "Score:" + (int)(playerScore * 3 - 5));
    }
    }
     
  19. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    if you will not destroy originl banana, so make another banana, that dont have destroy code in it and put it in the scene.


    highestscore = playerScore > highscore; this line make for me no sense. What do you want with this line ?

    maybe so ?

    if (playerScore > highscore)
    highestscore = playerScore;