Search Unity

Move Diagonally in 2D space?

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

  1. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    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. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    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:
    26
    Try this:

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

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    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:
    26
    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
    RuneShiStorm likes this.
  6. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    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:
    833
    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:
    833
    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.
     
    RuneShiStorm likes this.
  9. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    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. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    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. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    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:
    833
    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 ;)
     
    RuneShiStorm likes this.
  13. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    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.
     
    RuneShiStorm likes this.
  14. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Awesome! looking forward to it!
    Good luck !:D
     
    MisterSkitz likes this.
  15. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I ran out of time, the lag was a major issue but I've combated that pretty well. I'll be re-making the video again tomorrow and post it in here. Just know, I tried very hard to get it right for ya! But it is coming! See ya tomorrow! (Hopefully lol)
     
  16. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Ah! man thats awesome. very thankful! Im sure it will help alot of people. As far as I can tell there are very few tutorials on how to use Timelines!
     
  17. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I can make a video about Timelines but that is more for cut scenes. What I am going to do is show you an intricate form of movement. I have 102 lines of code so far designed. That's all I have the time for tonight, so once again, I will be trying to get this posted tomorrow. Even if you don't use this, you will find the content most useful, and that much, I guarantee! ;)
     
    RuneShiStorm likes this.
  18. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    ah i see! yes that sounds great. Im sure I can use the code and work around it as well. If nothing else I will learn something new :D
     
  19. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Any updates on the videos you where making?
    I sorta sorted out my problem but would still be interested to see how you turn timelines to reusable Prefabs :p
     
    MisterSkitz likes this.
  20. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Well I got stuck in one of my projects for weeks now so I'm trying to resolve this issue first because it is driving me insane!
    However, I wasn't going to use timelines. I'm using a collider to stop player movement. Then using a designed trail for the player to follow automatically. No matter how many flights of stairs you add, the player will make it to the top. Not only that but the code will be smart enough to know whether he's at the top of the stairs and will move back down.

    Implementing animations for stair climbing would be easy with my code.

    I haven't forgot about this because I have started the code. I'm working on an A.I. tutorial at the moment, which that portion is completed. My only problem is making the enemy face the player once detected. Typically an easy feat. However, the way I'm doing it is proving difficult because there's nothing to research on it!(The way I'm doing it)
     
  21. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    ah cool! WHere do you put your tutorials? Would love to check them out. I managed to fix the stairs thingy, but I'm always keen to see others doing it as well so I can learn more. Take your time, dont feel pressure from me! :p But please let me know when you're done with these tutorials. I'm staring to work on Enemy AI soon as well. i know I to make a fairly simple one but Would like to improve it a bit :p
     
    MisterSkitz likes this.
  22. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I post them on my YouTube channel. I'm about to start recording the A.I. tutorial for top down 2D perspective which will include player detection, wall detection, random movement, layer mask, line casting, and enemy focusing on player and firing with a fire rate.

    So far I've only done a few tutorials, probably nothing you'd find that useful (except maybe one instance where I talk about formatting custom screen resolution for PC/Mac design).

    I'll inbox you a link when I upload the A.I. video. I would love the feedback, amigo! :)
     
    RuneShiStorm and vakabaka like this.