Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Swipe Control Issue

Discussion in 'Scripting' started by unitynoob24, Sep 15, 2020.

  1. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Hey guys!

    I set up a simple POC using some swipe controls to launch a projectile. The game exists entirely in a canvas, originally using canvas overlay. My controls are very simple, basically leveraging OnPointerDown / OnPointerUp is how it works.

    I wanted to add a trail effect to the projectile which apparently cannot be done with an overlay canvas - so I switched to a Screen Space Camera canvas. It seems like everything is where it should be and behaving correctly; everything except my swipe controls. I got close to reworking it, but feel I am missing something as something doesn't feel as good as it did using overlay.

    Here is how I handled it with overlay:
    Code (csharp):
    1.  
    2. public void OnPointerUp(PointerEventData eventData){
    3.             direction = eventData.position - (Vector2)this.transform.position;
    4.             rb2d.AddRelativeForce(direction.normalized * speed, ForceMode2D.Impulse);
    5.         }
    6.  
    Running this code on screen space gives super odd results like the projectile is moving insanely fast, but adjusting speed doesn't seem to do anything, also playing with all rigid body 2d settings didn't help much at all. My guess is it is a coordinate system issue.

    Here is how I currently have it, it seems like the correct coordinate logic but like I was saying, it feels clunky or something - I had it feeling super smooth with the overlay canvas.
    Code (csharp):
    1.  
    2. public void OnPointerUp(PointerEventData eventData){
    3.             direction = eventData.position - (Vector2)this.transform.position;
    4.             Vector2 stwp = Camera.main.ScreenToWorldPoint(direction);
    5.             rb2d.AddRelativeForce(stwp * speed, ForceMode2D.Impulse);
    6.         }
    7.  
    Thanks guys!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    This is an excellent first hypothesis.

    To test it, I recommend making a script to debug some numbers in realtime, see what those numbers look like both before and after your screenspace change.
     
    unitynoob24 likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    The usual way to add a trail to something is with a trail renderer which lives in world space, not UI space, so it shouldn't matter what kind of Canvas you are using, because the trail renderer isn't on the canvas. Unless you specifically want the trail to be part of the UI?

    Edit: my bad just read this part^
     
    unitynoob24 likes this.
  4. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Thanks for the replies guys! I just solved it after doing a bunch of debug logs and then poking around the internet with more information!

    If anyone else is curious about swipe on canvas using render mode Screen Space - Camera, this is how I did it!
    Code (csharp):
    1.  
    2. public void OnPointerUp(PointerEventData eventData){
    3. Vector3 dragVectorDirection = (eventData.position - eventData.pressPosition).normalized;
    4. rb2d.AddRelativeForce(dragVectorDirection * speed, ForceMode2D.Impulse);
    5. }
    6.  
     
    Last edited: Sep 16, 2020