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

Why does my pointerdown event trigger only work for my player movement but not rotation?

Discussion in 'Scripting' started by Lethn, Jun 13, 2018.

  1. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public Rigidbody2D player;
    8.  
    9.  
    10.     public void MoveUp ()
    11.  
    12.     {
    13.  
    14.         player.AddForce ( transform.up * 10 );
    15.  
    16.         Debug.Log ("Player Moving");
    17.     }
    18.  
    19.     public void MoveDown ()
    20.  
    21.     {
    22.         player.AddForce ( transform.up * -10 );
    23.  
    24.         Debug.Log ("Player Moving");
    25.     }
    26.  
    27.     public void RotateLeft ()
    28.  
    29.     {
    30.         player.transform.Rotate ( 0, 0, 50 * Time.deltaTime );
    31.     }
    32.  
    33.     public void RotateRight ()
    34.  
    35.     {
    36.         player.transform.Rotate ( 0, 0, -50 * Time.deltaTime );
    37.  
    38.     }
    39. }
    40.  
    I have a basic movement setup for an android project I'm working on, it works, sort of, but I have pointerdown event triggers on all of my movement buttons and only the Up and Down buttons work but when I want to rotate the player it only will do it on one click like a normal event despite the event trigger definitely being there.

    Anyone got any ideas as to why this is happening?
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Bumping since it's been awhile if that's okay.
     
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Do you mean that you click once (on up/ down buttons) and it moves but when you click once (on left/ right buttons) it only rotates a single step?

    If so, then my guess would be that the Up/ Down is happening under physics and, therefore, will continue to move after the button click. The rotation, on the other hand, is being done 'manually' (so to speak) so will only happen once per click (assuming you are reacting to the single frame button down event).
     
    Lethn likes this.
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Oh I see! That explains it, so in order to get the rotation to work smoothly I just need to use the rigidboy rotation to fix that and then the rotation will continually apply.

    Thanks.
     
  5. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    That is what I am thinking, yes. Hope it helps. :)
     
  6. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Just to let people know, I solved the problem in the end using Rigidbody Velocity.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public Rigidbody2D player;
    8.  
    9.  
    10.     public void MoveUp ()
    11.  
    12.     {
    13.         player.velocity  = new Vector2 ( 0, 1 );
    14.     }
    15.  
    16.     public void MoveDown ()
    17.  
    18.     {
    19.         player.velocity  = new Vector2 ( 0, -1 );
    20.     }
    21.  
    22.     public void MoveLeft ()
    23.  
    24.     {
    25.         player.velocity  = new Vector2 ( -1, 0 );
    26.     }
    27.  
    28.     public void MoveRight ()
    29.  
    30.     {
    31.         player.velocity  = new Vector2 ( 1, 0 );
    32.     }
    33. }
    34.  
    Works very smoothly actually, ideally I may want to do something about the rotation properly but I'm happy. Just need to experiment more with incrementing the movement as you press down and maybe figure out how to stop it.and so on.
     
    Last edited: Jun 18, 2018
    Doug_B likes this.