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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Move around the star object

Discussion in '2D' started by KiviGameMaker, May 9, 2018.

  1. KiviGameMaker

    KiviGameMaker

    Joined:
    Mar 18, 2018
    Posts:
    22
    Hello guys.
    I did not find a solution to my problem, so I write it here.
    My problem:
    I need a player to move only through the black layer of the star (see screenshoot). It is possible ? Or if I have a polygon for example, I need a player to move only over the black polygon layer.

    Screenshoot:
    https://imgur.com/wkf39lS

    Thanks for every answer.
    Beginner power .
     
  2. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,068
    Okay.
    So the black outline that makes up the star, is like a star shaped path/maze?
    So in other words, you can walk in a star shaped path?
    But the white areas, are off limits, right?
     
  3. KiviGameMaker

    KiviGameMaker

    Joined:
    Mar 18, 2018
    Posts:
    22
    Yes man
     
  4. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    use an edge collider. you can then add points by double clicking on the green collider outline while youre editting it. i have no idea how to remove the points though xD

    you might have to play with the edge radius and positioning so that you cant force your way through
     
  5. FantasticGlass

    FantasticGlass

    Joined:
    May 31, 2016
    Posts:
    38
    I'd do it like this:
    Star (Empty Game Object)
    Star Sprite (Sprite)​

    To the Star Game Object add Two Edge Collider Components.
    Increase the Edge Radius a bit depending on how large or small your sprite is. If the edge radius is 0, then your player might move right through it depending on how you choose to move your player around.

    Click on the Edit Collider Button and position points on the edge collider in their correct spaces.

    See images here:
    https://imgur.com/gallery/LEfHlBE

    This will work if you move player using transform.position, but will be smoother if you use use rigidbody movement such as Rigidbody.AddForce or manipulate rigidbody.velocity directly.

    In this example I just manipulated the transform.position for simplicity.
    player is just a circle sprite with a circle collider.

    See player movement GIF here:
    https://imgur.com/gallery/jxFsjI0


    for the player I used this script:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class TransformMove : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     private float horizontal;
    10.     [SerializeField]
    11.     private float vertical;
    12.  
    13.     [SerializeField]
    14.     private float speed = 1f;
    15.  
    16.  
    17.  
    18.     void Update ()
    19.     {
    20.         GetInput();
    21.  
    22.         MovePlayer();
    23.     }
    24.  
    25.     private void GetInput ()
    26.     {
    27.         horizontal = Input.GetAxis("Horizontal");
    28.         vertical = Input.GetAxis("Vertical");
    29.     }
    30.  
    31.     private void MovePlayer ()
    32.     {
    33.         Vector3 newPosition = transform.position;
    34.  
    35.         newPosition.x += horizontal * speed * Time.deltaTime;
    36.         newPosition.y += vertical * speed * Time.deltaTime;
    37.                
    38.         transform.position = newPosition;
    39.     }
    40. }
    41.  
     
  6. FantasticGlass

    FantasticGlass

    Joined:
    May 31, 2016
    Posts:
    38
    You can delete a point by Control Clicking on the point you wish you remove.
     
    knobblez likes this.
  7. KiviGameMaker

    KiviGameMaker

    Joined:
    Mar 18, 2018
    Posts:
    22
    Thank you guys :)
     
    FantasticGlass likes this.