Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Convert input.GetKey into touch and hold function

Discussion in 'Editor & General Support' started by mykitaa, May 24, 2016.

  1. mykitaa

    mykitaa

    Joined:
    Feb 22, 2015
    Posts:
    14
    Hi,

    Here's what the game and the mechanic looks like:


    This is what I currently have:

    Code (CSharp):
    1.         if (Input.GetKey (KeyCode.Q))
    2.             angle = -Time.deltaTime * 128;
    3.         else if (Input.GetKey (KeyCode.W))
    4.             angle = Time.deltaTime * 128;
    5.         else
    6.         {

    This works very well when I run the game on a computer, but obviously not when I build it and run on android.

    I want to change the code so that the dark grey object moves not by pressing Q or W but by touching and holding.

    Could anyone please guide me on this one?
     
  2. Deleted User

    Deleted User

    Guest

    Shoot a ray from the touch position from the camera ?
     
  3. mykitaa

    mykitaa

    Joined:
    Feb 22, 2015
    Posts:
    14
    Oh. How to do this?
     
  4. Deleted User

    Deleted User

    Guest

    Unity has a example of this on there input touch API

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     void Update() {
    6.             if (Input.GetTouch(0).phase == TouchPhase.Began) {
    7.                
    8.                 // Construct a ray from the current touch coordinates
    9.                 RaycastHit hit;
    10.                 Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    11.                 if (Physics.Raycast(ray, out hit))
    12.                 {
    13.                     // Use the name or a tag object to make sure it's the dark grey one
    14.                    // If it is perform the moment code
    15.  
    16.                 }            
    17.         }
    18.     }
    19. }
    Something like that
     
  5. mykitaa

    mykitaa

    Joined:
    Feb 22, 2015
    Posts:
    14
    Hi, thanks. Doesnt seem to work for me.

    I tried to edit the original code:

    Code (CSharp):
    1.     // Update is called once per frame
    2.     void Update () {
    3.         float angle = 0;
    4.         bool player_stand;
    5.         bool player_stand_this;
    6.  
    7.         GameObject player = GameObject.Find("Player");
    8.         MoveCharacter script = player.GetComponent("MoveCharacter") as MoveCharacter;
    9.         player_stand = (script.StandingFloor.GetComponent("Rotating") != null);
    10.         player_stand_this = (script.StandingFloor == gameObject);
    11.  
    12.         if (!playerRot && player_stand) return;
    13.  
    14.         if (Input.GetKey (KeyCode.Q))
    15.             angle = -Time.deltaTime * 128;
    16.         else if (Input.GetKey (KeyCode.W))
    17.             angle = Time.deltaTime * 128;
    18.         else
    19.         {
    20.             float dst = Mathf.Floor((m_Angle + 45) / 90) * 90;
    21.             angle = dst - m_Angle;
    22.             if (Mathf.Abs(angle) > 1) angle *= 0.125f;
    23.             if (rotating && angle == 0) {
    24.                 rotating = false;
    25.                 Vector3 pos = transform.position;
    26.                 transform.position = new Vector3(Mathf.Round(pos.x), Mathf.Round(pos.y), Mathf.Round(pos.z));
    27.                 if (player_stand_this)
    28.                 {
    29.                     script.RecalcPlayerPos();
    30.                 }
    31.             }
    32.         }




    into this:

    Code (CSharp):
    1. // Update is called once per frame
    2.     void Update () {
    3.         float angle = 0;
    4.         bool player_stand;
    5.         bool player_stand_this;
    6.  
    7.         GameObject player = GameObject.Find("Player");
    8.         MoveCharacter script = player.GetComponent("MoveCharacter") as MoveCharacter;
    9.         player_stand = (script.StandingFloor.GetComponent("Rotating") != null);
    10.         player_stand_this = (script.StandingFloor == gameObject);
    11.  
    12.         if (!playerRot && player_stand) return;
    13.  
    14.         if (Input.GetTouch(0).phase == TouchPhase.Began) {
    15.             // Construct a ray from the current touch coordinates
    16.             RaycastHit hit;
    17.             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    18.             if (Physics.Raycast(ray, out hit))
    19.             {
    20.                 GameObject obj = hit.transform.gameObject;
    21.  
    22.                 {
    23.             float dst = Mathf.Floor((m_Angle + 45) / 90) * 90;
    24.             angle = dst - m_Angle;
    25.             if (Mathf.Abs(angle) > 1) angle *= 0.125f;
    26.             if (rotating && angle == 0) {
    27.                 rotating = false;
    28.                 Vector3 pos = transform.position;
    29.                 transform.position = new Vector3(Mathf.Round(pos.x), Mathf.Round(pos.y), Mathf.Round(pos.z));
    30.                 if (player_stand_this)
    31.                 {
    32.                     script.RecalcPlayerPos();
    33.                 }
    34.             }
    35.         }
    But nothing happens. It's not rotating. I must have done it all wrong
     
  6. Deleted User

    Deleted User

    Guest

    Yeah so basically the ray shoots out from the camera to where you are touching, the objects you want to hit need to have a collider on them. You can then sue that to check the tag or name to make sure its the dark one then perform the code.
    Code (CSharp):
    1. if(hit.gameObject.tag == "DarkGreyObject") {
    2.     // Do rotation
    3. }
    The issue im seeing is you want to rotate either negatively or positively, a single touch could only rotate in one direction. you will probably want another method for example you can click the piece then click either the left side or right side of the screen to rotate it by how ever much like 90 degrees or steps of 15 or 25.
     
  7. mykitaa

    mykitaa

    Joined:
    Feb 22, 2015
    Posts:
    14
    Yes I had this in mind but I thought it would be too complicated as I am only weeks into Unity. I've been building games using Buildbox, and that's a engine which requires no coding skills :) But I want to learn C# now. Thanks for helping me out :)

    I get this error as I try to add the string you wrote:

    Code (CSharp):
    1. Severity    Code    Description    Project    File    Line    Suppression State
    2. Error    CS1061    'RaycastHit' does not contain a definition for 'GameObject' and no extension method 'GameObject' accepting a first argument of type 'RaycastHit' could be found (are you missing a using directive or an assembly reference?)    UnityValley-master.CSharp    D:\DOWNLOADS\ny2\UnityValley-master\Assets\Rotating.cs    52    Active
    3.  
     
  8. Deleted User

    Deleted User

    Guest

    Whoops yeah use this and put it within the physics ray cast out bit:
    Code (CSharp):
    1. if(Physics.Raycast(ray, out hit))
    2. {
    3.   if(hit.collider.tag == "DarkGreyObject")
    4.    {
    5.         // Do rotation
    6.     }
    7. }
     
  9. mykitaa

    mykitaa

    Joined:
    Feb 22, 2015
    Posts:
    14
    Thanks. Still some muffins. I keep getting this error at line 48:

     
  10. Deleted User

    Deleted User

    Guest

    Encapsulate the touch code with this check first to make sure there is actually a touch if not then don't check for the touch.
    Code (CSharp):
    1. if (Input.touchCount > 0)
    2. {
    3.      //all the other code goes  in this
    4.      // if(Input.GetTouch(0).phase == TouchPhase.Begin) etc
    5.  
    6. }
     
  11. mykitaa

    mykitaa

    Joined:
    Feb 22, 2015
    Posts:
    14
    Okay, no errors now, working with this:

    Code (CSharp):
    1.  if (Input.touchCount > 0)
    2.         {
    3.            if (Input.GetTouch(0).phase == TouchPhase.Began) {
    4.                 // Construct a ray from the current touch coordinates
    5.                 RaycastHit hit;
    6.                 Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    7.                 if (Physics.Raycast(ray, out hit))
    8.                 {
    9.                     if (hit.collider.tag == "Floor") {
    10.                     }
    The tag of the dark grey object is "Floor", but nothing happens as I click it. (I am testing on computer, but a mouseclick equals a tap on a mobile device, right?)
     
  12. Deleted User

    Deleted User

    Guest

    No mouse input and touch are different you would be best testing it on the device, the code there creates a ray from the place you touch from the camera into the scene. it then checks if it has hit a collider and if it has it checks if that game object has the tag "Floor" (you set the tag on the object in the inspector). If it does then you can run a method, as discussed earlier you will probably want the object to maybe highlight then allow users to click on either the left or the right side of the screen to cause a rotation.

    Code (CSharp):
    1. if (hit.collider.tag == "Floor")
    2. {
    3.       RotateEnabled();              
    4. }
    With the method call you can maybe enable buttons for rotate left or right and these replace your w and q controls and allow you to call the following functions:

    Code (CSharp):
    1. public void RotateLeft()
    2. {
    3. angle = -Time.deltaTime * 128;
    4. }
    5.  
    6. public void RotateRight()
    7. {
    8. angle = Time.deltaTime * 128;
    9. }
    Once they have committed to the rotation, RotateEnable disables the buttons and highlight of the object and pops it in place and waits for the next time its tapped.