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. Dismiss Notice

Scripting themes for First Project

Discussion in '2D' started by Shiranui7, Nov 30, 2020.

  1. Shiranui7

    Shiranui7

    Joined:
    Nov 8, 2020
    Posts:
    3
    Hey folks,

    first things off, I'm a total beginner in Unity, yet I need to develop my first game project, so here I am.
    I've tried several approaches and methods yet C+ scripting is not my strength so far. Therefore it would be really nice if someone could give me some guidelines or hint me themes relative to my project in the documentation in order to advance further.

    The game itself is relatively simple I guess (concept image is linked somewhere here as well): you have these blue "platforms" (dots) the Player can reach by clicking on them. There are several obstacles hindering its path (orange cubes).

    Concept Img (https://ibb.co/Km5JXBK)


    That's all. I imagined the movement to be like a ray from dot to dot, if the ray hits an obstacle you respawn at the start (initial dot on the left). The obstacles aren't still but have some basic movements like up & down or circular paths.

    I was told to work with Raycasting, yet I after several attempts, I came to the conclusion that I don't understand the mechanic.
    The attempt that worked best so far was another one:

    // Update is called once per frame
    void Update()
    {
    if (!isMoving && Input.GetMouseButtonDown(0))
    {
    SetTargetPosition();
    }

    if (isMoving)
    {
    Move();
    }
    }
    void SetTargetPosition()
    {
    targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    targetPosition.z = transform.position.z;
    isMoving = true;
    }
    void Move()
    {
    transform.rotation = Quaternion.LookRotation(Vector3.forward, targetPosition);
    transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
    if(transform.position == targetPosition)
    {
    isMoving = false;
    }
    }
    Basically now I have a dot that moves on left-click wherever I want. And now I would need the other two interactions with the remaining dots and the obstacle :/. Yet I had no success so far introducing other scripting lines.

    Hope I expressed myself clear enough!
     
  2. luakGames

    luakGames

    Joined:
    Jul 13, 2020
    Posts:
    4
    hi Shiranui,

    I think that I understand what you are trying to do, but if not tell me!
    I am also a beginner to unity so this may not be what you need but I will try to help.

    if you are trying to detect when a line (the ray from the player) hits another point you could do it two ways (there may be more but I don't know them if there are)

    1. You could use collision detection (triggers) and colliders to tell when the line (which could have a box collider on it).
    this would allow you to be able to detect when the line hits the dots and then you could do your movement.
    (this method would also allow you to detect when the player (or line) hits the obstacles
    here is a code sample of which you can change around to fit your needs:

    private void OnTriggerEnter2D(Collider other)
    {

    //Check for a match with the specified name on any object that collides with your player
    if (other.gameObject.name == "obstacle one")
    {
    // if the object matches then do stuff
    if (Input.GetKey(KeyCode.Q))
    {
    // do crap
    }
    }

    2. The other way that you could do this to use ray-casting like you are. I don't know much about ray-casting so I cannot help there, if this doesn't help make sure to go to unity answers!

    Im not sure if this was what you were wanting.
     
    Shiranui7 likes this.
  3. Shiranui7

    Shiranui7

    Joined:
    Nov 8, 2020
    Posts:
    3
    Yes, exactly this is one of the issues I tried to find out! Also, may I ask, the second problem I encountered is the player's movement. I tried incorporating this "waypoint" system, where the player is able to move along these different dots around the level. Do you have any idea how I can script this. For now I have a linear or interpolated movement that is possible anywhere on the stage, yet I would like only certain spots to call an action.
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Here is a quick and easy tutorial for Raycasts in 2D. As for your waypoint movement system, after you watch that tutorial linked below, you should understand how to move your object to each point. Basically, if the raycast hits an object (like a movement point) you want, then you can change the object's transform position to that new point.

     
    Shiranui7 likes this.
  5. Shiranui7

    Shiranui7

    Joined:
    Nov 8, 2020
    Posts:
    3
    Thanks! The video is indeed really insightful. I now managed to move my player from spot to spot and there is a collision going on when it hits an obstacle.
     
    luakGames and Cornysam like this.