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

Options for coding touch with gameobjects

Discussion in 'Scripting' started by AndyNeoman, Mar 5, 2015.

  1. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Hi All,

    I am hoping for a little community inspiration after running into a couple road blocks with a game design.

    The game idea is a simple 3d level where the player is static in the middle of the environment and has enemie flying around him. I wanted to be able to hit the bugs scoring points. I was looking at raycast but when I did debug drawline the line stayed on one half of the screen only.

    I also want to draw a cylinder from the the player to the touch position which has a collider on it to interact with enemies.

    I just had to come away from the code for a minute as I have looked at several options and none seem to be working correctly.

    Would you suggest Raycast, Line renderer, stretch a cylinder prefab to position or stack a number of them or can anyone suggest an alternative?

    Thanks for any help - I am just going to take five and hope someone can point me in the right direction.

    Thanks again - Andy
     
  2. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Thought I would add some code to try and spure debate. Sorry it is a bit messy part of my problem i think is I have tried several approaches in the same script.

    The first problem I have is the ray only appears on the right side and top of the screen.

    If i touch the screen on the bottom or left it still debug line draws on the right.

    Any idea why this could be?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RaycastTongue : MonoBehaviour
    5. {
    6.     public Transform tongue;
    7.     public float tongueLength;
    8.     public float speed = 0.1f;
    9.     public float lineDrawSpeed;
    10.     private LineRenderer lineRenderer;
    11.     private float counter;
    12.     private float dist;
    13.     RaycastHit hitInfo;
    14.     Ray ray;
    15.     Vector3 tongueDirection;
    16.     Vector3 touchPos;
    17.     // Use this for initialization
    18.     void Start ()
    19.     {
    20.  
    21.         ray.origin = transform.position;
    22.         ray.direction = Input.GetTouch(0).position; //tongueDirection; //Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    23.         lineRenderer = GetComponent<LineRenderer>();
    24.         lineRenderer.SetPosition (0,transform.position);
    25.         lineRenderer.SetPosition (1,transform.position);
    26.         dist = Vector3.Distance (transform.position,tongueDirection);
    27.     }
    28.    
    29.     // Update is called once per frame
    30.     void Update ()
    31.     {
    32.  
    33.         //if screen touched
    34.         if (Input.touchCount > 0 )
    35.         {  
    36.             //touchPos = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position);
    37.             tongueDirection = Input.GetTouch(0).position;
    38.  
    39.             //ray.origin=tongue.position;
    40.             //ray.direction=Input.GetTouch(0).position; //Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    41.             //RaycastHit hitInfo;
    42. //            Vector3 temp = tongue.localScale + (tongue.position-tongueDirection);
    43. //            tongue.localScale = temp;
    44.             //create raycast from tongue position
    45. //            Debug.Log ("TransPos "+transform.position);
    46. //            Debug.Log ("TongDir "+tongueDirection);
    47. //            Debug.Log ("Tonglength "+tongueLength);
    48. //            Debug.Log ("Ray "+ray);
    49. //            Debug.Log ("Hitinfo "+hitInfo.point);
    50.             int hitForce = 10;
    51.  
    52.             if (Physics.Raycast(transform.position, tongueDirection,tongueLength) !=null) //,tongueLength))
    53.             {
    54.                 Debug.Log ("you are casting a ray");
    55.                 if(counter<dist)
    56.                 {
    57.                     counter += .1f / lineDrawSpeed;
    58.                 float x = Mathf.Lerp (0,dist,counter);
    59.                 Vector3 pointAlongLine = x * Vector3.Normalize(tongueDirection-transform.position)+ tongueDirection;
    60.                 lineRenderer.SetPosition (1,pointAlongLine);
    61.                
    62.                 }
    63.                     Debug.DrawRay(transform.position,tongueDirection,Color.black,1);
    64.                 //Debug.DrawRay(transform.position,touchPos,Color.yellow,1);
    65.  
    66.                 if (hitInfo.rigidbody != null)
    67.                 {  
    68.                     GetComponent<AudioSource>().Play();
    69.                     hitInfo.rigidbody.AddForce(-ray.direction * hitForce);
    70.                     Debug.Log ("Hit ButterFly POW "+hitInfo.rigidbody);
    71.                 }
    72.             }
    73.         }  
    74.     }
    75. }
    76.  
     
  3. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Anyone offer any advice for this issue?