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

Tethered movement

Discussion in 'Scripting' started by KowlDoog, Oct 12, 2020.

  1. KowlDoog

    KowlDoog

    Joined:
    Aug 17, 2020
    Posts:
    10
    Hey guys,
    I'm having a hard time thinking of other ways to word this so I can just look it up, but I'm trying to make a tethered point-to-point movement from the likes you'd see in the DS game Ghosttrick, where you have a central anchor and tethered distance that you can reach that lets you move to other anchors.

    So far I've deduced I need some kind of range limiter for the area you can move around (can a circle collider be inverted so you're interacting with the inside of it rather than the outside?) and to return to the center point of the anchor if you let go of a direction. Think of it like an analogue stick where it'd ping back to the center.

    any help would be appreciated,
    cheers guys
     
    Last edited: Oct 12, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Lots of ways to approach this. I haven't played the game you reference above, but here are some different approaches depending on how you're doing:

    - every time you move, check the range to the tether center, and if it goes beyond it, hard-clip it

    - when you get near the above, slow your motion down so it has a softer edge limit feel

    - use physics and when beyond a distance, start pushing you back (if you're using physics)

    I suppose you could do it with a collider, if you're using physics, but I think I would reach for the explicit distance calculation above instead.
     
    KowlDoog likes this.
  3. KowlDoog

    KowlDoog

    Joined:
    Aug 17, 2020
    Posts:
    10
    So far I've set it up to use rigidbody2D physics, I incorrectly put sphere collider instead of circle in the first post.

    I'm debating whether I even need it a rigidbody or not because movement is limited between the anchor points, but I'm still trying to think of terms to search that I can reference to that's similar
     
  4. KowlDoog

    KowlDoog

    Joined:
    Aug 17, 2020
    Posts:
    10
    I've mocked this up quick so it's clear on what I want to achieve -

    Movement is restricted so you're only allowed to move within the radius of the colliders, residing in the center, if you're between two intersecting colliders, you're sent to that new points center
     

    Attached Files:

  5. KowlDoog

    KowlDoog

    Joined:
    Aug 17, 2020
    Posts:
    10
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
  7. KowlDoog

    KowlDoog

    Joined:
    Aug 17, 2020
    Posts:
    10
    my bad

    Code (CSharp):
    1. public Transform player; // get the player transform, or w/e object you want to limit in a circle
    2. public Vector3 circleCenter; // this is a location that is set to the middle of my world, it will be the center of your circle.
    3.  
    4. Void Update()
    5. {
    6.         float radius 20f; // this is the range you want the player to move without restriction
    7.         float dist = Vector3.Distance(player.position, circleCenter); // the distance from player current position to the circleCenter
    8.      
    9.         if (dist > radius)
    10.         {
    11.             Vector3 fromOrigintoObject = player.position - circleCenter;
    12.             fromOrigintoObject *= radius / dist;
    13.             player.position = circleCenter + fromOrigintoObject;
    14.             transform.position = player.position;
    15.         }
    16. }
    it keeps the player within a radial boundary but i just need to put that into an object, my idea was to use the vertex3 into a transform object but c# doesn't work like that, i've not found a workaround so i think i need to approach it differently? I'm used to basic coding things I would consider this the first "advanced" thing I've taken on for an idea
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    I don't think you want circleCenter to be the center of the world. You want it to have the position of the anchor you are starting this particular move from, right? You can make a blank GameObject there and drag its reference into the script.
     
    KowlDoog likes this.
  9. KowlDoog

    KowlDoog

    Joined:
    Aug 17, 2020
    Posts:
    10
    it has the same issue of that the gameobject can't be called from a Vector3, i also tried it with searching for tags. i feel like i've over complicated it in my head and I've been at this for hours, i'll have a break and see what else there is
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    I know what you mean but don't think of it this way. You need to understand how Unity is structured:

    - GameObjects are the ONLY thing you see in the scene graph. That's it. Nothing else.

    - They ALL have components. ALL of them have a Transform (or RectTransform).

    - One of the fields in a Transform is its position.

    - That position is a Vector3.

    THAT is what you compare to the player position.