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

Worldpoint-Screenpoint. bamboozled

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

  1. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Hey guys,

    I am pretty new to GameDev with only a few months of experimetning but I have used raycast and linecast before from the camera/FPS but I am damned if I can get it to work from a stationary character on the screen.

    Whenever I cast a ray it does not go anywhere near the mouse point (which i want it to).
    so to sum up.

    I want to through a linecast out from the players position to the input of the mouse left button. It sounds so easy when I type it but it has had me all over the place for 3 days. Please someone put me out of my misery with a bit of advice. \panic stricken sob.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TongueRender : MonoBehaviour
    6. {
    7.     public Transform MouseDestination;
    8.     LineRenderer line;
    9.     Vector3 mousePos ;
    10.     void Start ()
    11.     {
    12.         line = gameObject.GetComponent<LineRenderer>();
    13.         line.enabled = false;
    14.         mousePos = Input.mousePosition;
    15.     }
    16.     void Update ()
    17.     {
    18.         if(Input.GetButtonDown("Fire1"))
    19.         {
    20.             StopCoroutine("FireLaser");
    21.             StartCoroutine("FireLaser");
    22.         }
    23.     }
    24.     IEnumerator FireLaser()
    25.     {
    26.         line.enabled = true;
    27.        
    28.         while(Input.GetButton("Fire1"))
    29.         {
    30.             Ray ray = new Ray(transform.position, Camera.main.ScreenToWorldPoint (mousePos) );
    31.             RaycastHit hit;
    32.             //Instantiate (MouseDestination,Camera.main.ScreenToWorldPoint (mousePos),Quaternion.identity);
    33.             line.SetPosition(0, ray.origin);
    34.            
    35.             if(Physics.Raycast(ray, out hit, 100))
    36.             {
    37.                 line.SetPosition(1, hit.point);
    38.                 if(hit.rigidbody)
    39.                 {
    40.                     hit.rigidbody.AddForceAtPosition(transform.forward
    41.                                                      * 10, hit.point);
    42.                 }
    43.             }
    44.             else
    45.                 line.SetPosition(1, ray.GetPoint(100));
    46.            
    47.             yield return null;
    48.         }
    49.        
    50.         line.enabled = false;
    51.     }
    52.  
    53.     void OnTriggerEnter(Collider GameObject)
    54.     {
    55.         if (GameObject.tag =="Enemy")
    56.         {
    57.             Debug.Log ("enemy hit");
    58.         }
    59.     }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You only set mousePos once in start

    It is not a reference to the mouse position, it is just a value which stays whatever it is when it is set.
    Keep updating it

    Also, consider using camera.screenPointToRay
     
    AndyNeoman likes this.
  3. AndyNeoman

    AndyNeoman

    Joined:
    Sep 28, 2014
    Posts:
    938
    Thanks for that - I have been trying to get camera.screenPointToRay working but there is no camera on my player - the ray just goes from the centre of my screen instead of the player position.

    Big thumbs up- Thankyou, screenPointToRay is now working. No idea why this wasn't working three days ago when I did it initially but you did the trick :)
     
    Last edited: Mar 9, 2015