Search Unity

Move Diagonally in 2D space?

Discussion in '2D' started by hereder, Oct 8, 2019.

  1. hereder

    hereder

    Joined:
    Apr 28, 2017
    Posts:
    158
    Hi all!
    I'm trying to move my player (automatically) up these stairs on Button press..
    If the Player enter the boxcollider and press F he will be "transferred" to the top of the stairs by himself.
    I can't figure out how to find out how diagonal Float works :S if it even exist?


    My left and right movements are simple.

    Code (csharp):
    1.  
    2.            if (_moveDirection.x < 0)
    3.             {
    4.                 transform.eulerAngles = new Vector3(0, 180, 0);
    5.                 isFacingRight = false;
    6.             }
    7.             else if (_moveDirection.x > 0)
    8.             {
    9.                 transform.eulerAngles = new Vector3(0, 0, 0);
    10.                 isFacingRight = true;
    11.             }
    12.  
    But, since I don't want the player to be in control while walking up the stairs I thought I could turn of the playercontroller and set its gravity to 0 so it ignores physics...

    Code (csharp):
    1.  
    2. _characterController.GetComponent<PlayerController>().enabled = false;
    3. gravity = 0;
    4.  



    Is there an easy way to move the player diagonally upwards?
    Thanks in advnace!
     
  2. hereder

    hereder

    Joined:
    Apr 28, 2017
    Posts:
    158
    UPDATE: this simple script works... It takes me up the stairs. But is sorta "teleport" the player.
    Is it possible to make it smooth? Over the spam of 4 seconds for example?

    Code (csharp):
    1.  
    2.  
    3.       if (Input.GetKeyDown(KeyCode.J))
    4.             {
    5.                 transform.Translate(8.0f, 7.6f, 0.0f);
    6.             }
    7.  
    8.  
     
  3. Ledesma099

    Ledesma099

    Joined:
    May 15, 2018
    Posts:
    21
    Try this:

    transform.position = Vector2.MoveTowards (origin.postion, target.position, speed * Time.deltaTime);
     
    vakabaka likes this.
  4. hereder

    hereder

    Joined:
    Apr 28, 2017
    Posts:
    158
    Thanks. What exactly is speed, origin and target?
    Where to I specify them? Like this?
    Code (csharp):
    1.  
    2.     public float speed;
    3.     public Vector2 origin;
    4.     public Vector2 target;
    5.  
    Or inside start?
     
  5. Ledesma099

    Ledesma099

    Joined:
    May 15, 2018
    Posts:
    21
    Sorry for answering now.

    The origin in this case will be the transform of the player.

    The target is the point that your player needs to reach.

    Speed is the impulse that will be given to the player to move him to the target.

    For the target, I don't know if you use a game object in your scene as a point reference to move your player.
     
    Last edited: Oct 16, 2019 at 3:18 AM
    hereder likes this.
  6. hereder

    hereder

    Joined:
    Apr 28, 2017
    Posts:
    158
    thanks!
    I Managed to solve it with Timeline. So the player will enter an "animation event" when he walks up the stairs.
    All my previous problems where hard to solve because of physics etc :S so it was to complicated (as a noob) to mess around with transform etc :S But now it works the way I want... ish... I think. workling on it now!
     
  7. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    519
    On every stair case you should put two empty objects: One at the bottom with an IsTrigger collider and at the top with another IsTrigger collider. Then you simply code your player to automate to the opposite collider.(You wouldn't want player.transform.position = stairSpawn.transform.position because this would teleport. You'd want it to transition smoothly, I'll provide an example, gimmie a few...) I'd use OnTriggerStay() to present a UI panel with an option to go up or down accordingly pop up. For example:

    a UI panel box pops up with text: Do you want to proceed?

    Offer two choices: Yes or No buttons

    If Yes, player goes to the next spot. If no, the box closes and the collider is disabled for (x) amount of seconds. That way your player has a chance to escape the collider. And yes, the player will automatically move towards the next zone without any additional coding. However, to perfect it's path, you may need to adjust the transform.position of each empty. Also adjusting the colliders Scale to insure the player can still contact it.
     
    Ledesma099 likes this.
  8. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    519
    I actually thought about it and decided I'll make a video about this in the next couple days. Would be incredibly difficult to explain but extremely simple to show you an example.
     
    hereder likes this.
  9. hereder

    hereder

    Joined:
    Apr 28, 2017
    Posts:
    158
    lol, you should make it ;P Your ideas sound good and people might want it! :D
    I made it work now. I have a MainObjects and 2 child objects.
    If the player is on top of the stairs it will go down, and the otherway around. Problem is that both stair-objects are active at once and the boxcolliders overlap each other. Meaning, if I stand on top of the stairs and touching the bottom stairs boxcollider, it will wrap the player downstairs and walk upstaris again. I thought I needed the Boxcollider so the correct animation would play out as long as he is in the animation... But I guess I dont need the boxcollider beause the Animation is played inside the animatione vent... Yeah,... you're right.. this stuff is incredibly diffifult to explain :p
     
    MisterSkitz likes this.
  10. hereder

    hereder

    Joined:
    Apr 28, 2017
    Posts:
    158
    Edit: this is my script in case anyone need help...

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.Playables;
    7. public class EventScript : MonoBehaviour
    8. {
    9.     public PlayableDirector timeline;
    10.     private bool fix = false;
    11.     public Animator playerAnimator;
    12.     public RuntimeAnimatorController playerAnim;
    13.     public PlayableDirector director;
    14.     // Use this for initialization
    15.     void OnEnable ()
    16.     {
    17.         timeline = GetComponent<PlayableDirector>();
    18.         playerAnim = playerAnimator.runtimeAnimatorController;
    19.         playerAnimator.runtimeAnimatorController = null;
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update ()
    24.     {
    25.     if (director.state != PlayState.Playing && !fix)
    26.         {
    27.             fix = true;
    28.             playerAnimator.runtimeAnimatorController = playerAnim;
    29.         }
    30.     }
    31.     void OnTriggerStay2D(Collider2D collider)
    32.     {
    33.         if (collider.tag == "Player" && Input.GetKeyDown(KeyCode.F))
    34.         {
    35.             startEvent();
    36.         }
    37.     }
    38.     //void OnTriggerExit2D(Collider2D collider)
    39.     //{
    40.     //    timeline.Stop();
    41.     //}
    42.     public void startEvent()
    43.     {
    44.         timeline.Play();
    45.     }
    46. }
    47.  
    48.  
    49.  
    50.  
     
    MisterSkitz likes this.
  11. hereder

    hereder

    Joined:
    Apr 28, 2017
    Posts:
    158
    Oh btw, I made it work but at the same time it doesn't work if I move the stairs somewhere else... If you make a video, please show how we can use the same Timeline "event" in many different places.. :)
    It is as if the Animation event is "registered" to the place where its made on the scene. So If I move the stairs somewhere else, the player will teleport back to where I made the whole event the first time.. Ideally I would like to turn these stairs into a Prefab so i can use it somewhere else as well.. :)
     
    MisterSkitz likes this.
  12. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    519
    I'm definitely going to hook you up with a video to where you will be able to make it a prefab. I do work two full time jobs, so bare with me and hang tight, amigo! I got'cha on this asap ;)
     
    hereder likes this.
  13. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    519
    Realistically it'll be Sunday before I can get the video made. I work tomorrow 5am-2pm, then I have a photo/video shoot. Might work 5-2 on Saturday but not sure. Definitely off on Sunday. So between Sat night - Sunday night I should be able to come through. I think you'll like my approach. ;)
    Worth a watch.
     
    hereder likes this.
  14. hereder

    hereder

    Joined:
    Apr 28, 2017
    Posts:
    158
    Awesome! looking forward to it!
    Good luck !:D