Search Unity

Left mouse click input

Discussion in '2D' started by yazanSYR, May 27, 2020.

  1. yazanSYR

    yazanSYR

    Joined:
    May 27, 2020
    Posts:
    4
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    public Rigidbody2D rb;
    public float ForwardForce = 500f;
    public float UpForce = 500f;
    // Update is called once per frame
    void FixedUpdate()
    {
    Vector2 vf = new Vector2(ForwardForce * Time.deltaTime,0);
    rb.AddForce(vf);
    Vector2 vu = new Vector2(0, UpForce * Time.deltaTime);

    if (Input.GetKey("w") )
    {
    rb.AddForce(vu);
    }
    }

    So I have this code written to move my sprite(player) up by pressing w on my keyboard but I want this game to be on android and that it would be detected by the input of the person touching anywhere on his Android screen and I heard that I should have left mouse click which is equivalent to it when I release the app for android, but I don't know how to change this code so that the input would be by clicking on the mouse or clicking on android screen to add this upward force using if statement. (Game is 2D btw and I don't know why on preview it doesn't show my code to be indented but everything is perfectly indented)
    }
     
  2. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    Code (CSharp):
    1.  Input.GetMouseButtonDown(0)
    2.            
    3.  
    4.  
    for left click
     
  3. krabbypatty123

    krabbypatty123

    Joined:
    Jan 11, 2020
    Posts:
    4
    do you know how can i turn this script touch controlled please explein every little detaile like where can i take the ui from and what do i need to change in the input please help me i have been stuck with this and i have no idea
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playercombat : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.  
    9.  
    10.  
    11.     public Transform AttackPoint;
    12.  
    13.     public float AttackRange = 0.5f;
    14.  
    15.     public LayerMask enemyLayers;
    16.  
    17.     public int attackDamage = 40;
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (Input.GetKeyDown(KeyCode.Space))
    22.         {
    23.             Attack();
    24.         }
    25.     }
    26.  
    27.  
    28.     void Attack()
    29.     {
    30.         animator.SetTrigger("Attack");
    31.  
    32.         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(AttackPoint.position, AttackRange, enemyLayers);
    33.  
    34.         foreach(Collider2D enemy in hitEnemies)
    35.         {
    36.             enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
    37.         }
    38.     }
    39.  
    40.  
    41.  
    42.  
    43.     void  OnDrawGizmosSelected()
    44.     {
    45.  
    46.         if (AttackPoint == null)
    47.             return;
    48.         Gizmos.DrawWireSphere(AttackPoint.position, AttackRange);
    49.     }
    50.  
    51.      
    52.     }
    53.