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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GameObject Follow Mouse position

Discussion in 'Scripting' started by moliminous, Nov 22, 2015.

  1. moliminous

    moliminous

    Joined:
    Oct 16, 2015
    Posts:
    70
    So I'm trying to have a game object follow the mouse position but i was using out dated tutorials and guides so i had to come up with my own code. Sadly i'm kinda stuck because it tells me direction isn't part of unity vector 3?
    ray is the original code, but ray won't follow mouse positions, mouseworldposition works fine though.
    #pragma strict
    var ball:GameObject;
    function Start () {

    }
    private var hits:RaycastHit[];
    var hit:RaycastHit;
    private var ray:Ray;
    function Update () {
    var mouseWorldPosition : Vector3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    ray=Camera.main.ScreenPointToRay(Input.mousePosition);

    //Debug.DrawLine(transform.position, mouseWorldPosition, Color.red);
    hits=Physics.RaycastAll(Camera.main.transform.position,ray.direction,50);
    for (var i = 0;i < hits.Length; i++){
    hit = hits;
    if(hit.collider.tag=="screen")
    {
    ball.transform.position=hit.point;
    }
    }
    }
     
  2. xXSilverswordXx

    xXSilverswordXx

    Joined:
    Nov 20, 2015
    Posts:
    7
    dont you need to put this in script tags or on pastebin or something