Search Unity

Problem with creating 2D Rpg for Android

Discussion in '2D' started by sackta03, Apr 18, 2019.

  1. sackta03

    sackta03

    Joined:
    Apr 18, 2019
    Posts:
    3
    After the player changing location (moved from the one scene to another), player running in one direction, if using keyboard everything are normal, but when i uses buttons on screen, there is such a problem. Plz help if you can, thanks.(I'm rookie)
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Please explain your problem in more detail. Explain exactly what is going wrong. What you want it to be doing. What errors are popping up, if any. What you have tried doing to solve the issue. If you think its a code issue, post your code (using code tags please) upload_2019-4-18_16-37-0.png

    Users cannot help with vague issues of "It isnt working on mobile". Also, place debug.logs in your code to help yourself finding the issue(s).
     
  3. sackta03

    sackta03

    Joined:
    Apr 18, 2019
    Posts:
    3
    When I changing scene by special trigger player is running in one way. But when I off script and using another, without crossplatform, its working.


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityStandardAssets.CrossPlatformInput;
    3. public class PlayerCtrl : MonoBehaviour
    4. {
    5.     public float moveSpeed;
    6.     private float currentMoveSpeed;
    7.     public float diagonalMoveModifier;
    8.  
    9.     private Animator anim;
    10.     private Rigidbody2D myRigidbody;
    11.  
    12.     private bool playerMoving;
    13.     public Vector2 lastmove;
    14.  
    15.     private static bool playerExists;
    16.  
    17.     private bool attacking;
    18.     public float attackTime;
    19.     private float attackTimeCounter;
    20.  
    21.     public string startPoint;
    22.  
    23.     public bool canMove;
    24.  
    25.     // Start is called before the first frame update
    26.     void Start()
    27.     {
    28.         anim = GetComponent<Animator>();
    29.         myRigidbody = GetComponent<Rigidbody2D>();
    30.  
    31.         if(!playerExists)
    32.         {
    33.             playerExists = true;
    34.             DontDestroyOnLoad(transform.gameObject);
    35.         } else
    36.         {
    37.             Destroy(gameObject);
    38.  
    39.         }
    40.  
    41.         canMove = true;
    42.     }
    43.  
    44.     // Update is called once per frame
    45.     void Update()
    46.     {
    47.         playerMoving = false;
    48.  
    49.         if(!canMove)
    50.         {
    51.             myRigidbody.velocity = Vector2.zero;
    52.             return;
    53.         }
    54.  
    55.         if (!attacking)
    56.         {
    57.  
    58.  
    59.  
    60.                 if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
    61.                 {
    62.  
    63.                     //transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
    64.                     myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * currentMoveSpeed, myRigidbody.velocity.y);
    65.                     playerMoving = true;
    66.                     lastmove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    67.  
    68.                 }
    69.  
    70.                 if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
    71.                 {
    72.  
    73.                     //transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
    74.                     myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw("Vertical") * currentMoveSpeed);
    75.                     playerMoving = true;
    76.                     lastmove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
    77.  
    78.                 }
    79.  
    80.                 if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
    81.                 {
    82.  
    83.                     myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
    84.  
    85.                 }
    86.                 if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
    87.                 {
    88.  
    89.                     myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
    90.  
    91.                 }
    92.  
    93.                 if (Input.GetKeyDown(KeyCode.J))
    94.                 {
    95.                     attackTimeCounter = attackTime;
    96.                     attacking = true;
    97.                     myRigidbody.velocity = Vector2.zero;
    98.                     anim.SetBool("Attack", true);
    99.                 }
    100.  
    101.             if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5f && Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0.5f)
    102.             {
    103.                 currentMoveSpeed = moveSpeed * diagonalMoveModifier;
    104.             } else
    105.               {
    106.                 currentMoveSpeed = moveSpeed;
    107.             }
    108.  
    109.         }
    110.  
    111.         if (attackTimeCounter > 0)
    112.         {
    113.             attackTimeCounter -= Time.deltaTime;
    114.         }
    115.  
    116.         if (attackTimeCounter <= 0)
    117.         {
    118.             attacking = false;
    119.             anim.SetBool("Attack", false);
    120.         }
    121.  
    122.         anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
    123.         anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
    124.         anim.SetBool("PlayerMoving", playerMoving);
    125.         anim.SetFloat("LastMoveX", lastmove.x);
    126.         anim.SetFloat("LastMoveY", lastmove.y);
    127.     }      
    128.  
    129. }
     
    Last edited: Apr 19, 2019
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    So, if i am understanding correctly, the above script is working without crossplatform being active? And when Crossplatform is active, it isnt working? When you say "player is running in one way", is that supposed to be happening or no? Is he supposed to be able to go both directions?
     
  5. sackta03

    sackta03

    Joined:
    Apr 18, 2019
    Posts:
    3
    Yes when I use crossplatform its isnt working and yes he supposted to run in both diractions