Search Unity

Can someone point me in the correct direction for this?

Discussion in 'Scripting' started by Skynetworks, Oct 1, 2014.

  1. Skynetworks

    Skynetworks

    Joined:
    Sep 28, 2014
    Posts:
    9
    I'm looking to increase on the code of the space shooter since I have always liked those games as a kid, but my problem lies with the player movement. I have never liked space shooters that contained me to the left and right sides of the screen. I want a old school Mario Bros (the one with just the pipes and crabs/turtles) style movement, where when you go to the right side of the screen you come out the left. I am not sure how to search that without describing what it is. Any help would be great. Even a link to read more on the subject would suffice.

    I thank you!
     
  2. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    All you have to do is check the left/right position of the ship. If it's less than 0 (left side of screen), you set the position of the ship to be on the right side of the screen. If it's greater than the position of the right side of the screen, you set it to 0 so it's on the left side of the screen.

    transform.x gives you the left/right position.
     
    Skynetworks likes this.
  3. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    Simplest way to do this is to define scene boundaries.

    Probably the easiest way to do this for beginner is to place 4 gameobjects to the scene called e.g left, right, top, bottom boundary. then when player passes the boundary you simply change players position in that axis to the opposite boundary.

    Easily adjustable and fairly simple.
     
    Skynetworks likes this.
  4. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    If it's just a 2D thing I'd just do it with a couple if statements and be done with it. For 3D, yes, probably Vipsu's suggestion is much better than mine.
     
    Skynetworks likes this.
  5. Skynetworks

    Skynetworks

    Joined:
    Sep 28, 2014
    Posts:
    9
    Thank you for your quick reply's. Yes its 3d with orthographic view. Ill look into Vipsu's post
     
  6. Skynetworks

    Skynetworks

    Joined:
    Sep 28, 2014
    Posts:
    9
    This is what I have and it obviously isnt working. Im not sure how I specify the players game object in the script. (I have this attached to my boundary. Is this supposed to be attacked to my player as well ?


    Code (CSharp):
    1. function Update ()
    2.  
    3.  
    4. {
    5.     void OnTriggerExit(Collider other)
    6.     {
    7.          player.gameObject transform.position = Vector3(6,0,0);
    8.  
    9.     }
    10. }
    11.  
     
  7. Skynetworks

    Skynetworks

    Joined:
    Sep 28, 2014
    Posts:
    9
    // Wrap Screen
    if (transform.position.x <= -7.5f)
    {
    transform.position = new Vector3(7.4f, transform.position.y, transform.position.z);
    }
    else if (transform.position.x >= 7.5f)
    {
    transform.position = new Vector3(-7.4f, transform.position.y, transform.position.z);
    }