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

mouse.Position

Discussion in 'Scripting' started by Findo, Jun 12, 2014.

  1. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    How would I get the position of where the mouse is? Like, in Vector3, not Vector2

    In an old engine called ROBLOX, I would convert it like this:


    new Vector3 mouse_pos = new Vector(mouse.Hit.p);
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,936
  3. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    Not working the way I want.
    First of all, I want the center to be 0.
    Second, it's 2d, not 3d, third of all, i think its to sensitive
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    An example of what (I think) you want. Finding the position the mouse is hovering over.

    Code (csharp):
    1.  
    2. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    3. RaycastHit hit;
    4. if (Physics.Raycast(ray, out hit, 100)) {
    5.     // hit.point is the value you're looking for
    6. }
    7.  
    Keep in mind there needs to be a collider for it to hit.
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,936
  6. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    So uh, how would I put the transform at hit.point or whatever?
     
  7. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Code (csharp):
    1.  
    2. transform.position = hit.point;
    3.  
    That's my best guess, I don't know what you're trying to do.
     
  8. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    Yeah, I tryed that, but um.... it's weird. It like.... keeps on teleporting to the main camera and going back in an endless loop when im not moving my mouse at all. :mad:
     
  9. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Post your code and what you are trying to accomplish.
     
  10. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    I am trying to accomplish a cube following the mouse with Vector3.

    using UnityEngine;
    using System.Collections;

    public class asdfasdf : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit, 100)) {
    // hit.point is the value you're looking for
    transform.position = hit.point;
    }
    }
    }


    Problem: It does not stay at the mouse unless I take my mouse off the object. If i keep it on the object with my mouse on it, the cube moves towards the main camera, and once reached teleports back to my mouse's position and starts over again.
     
  11. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  12. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I'm guessing that the object you are trying to move also has a collider on it, so after the first frame the Raycast is now hitting that object. You will want to keep your Raycast from hitting that object by either using a LayerMask or assigning it to the 'ignore raycast' layer.
     
  13. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    ty its fixed ur the best :D
     
  14. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Nope, it's still needs the tag.
     
  15. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    What...?
    Its fixed.
     
  16. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Nope, it is not fixed yet. Which post are you talking about?
     
  17. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    Ok, then, if you want, you can help me fix it.
    I'm making a game where you have a like shop and can build things.
    It works great, so far, but ignore raycast makes it so it does not detect the brick I put down, therefore I cannot stack the bricks. How can I fix this?
     
  18. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You can disable the collider of the brick you are placing, then enable it after it has been placed.

    EDIT: This is instead of placing it in the ignore raycast layer.
     
  19. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    Daniel, unfortunately, it does not work when i activate and disable the collider. It still glitches out and teleports to the camera. Anyways, it is starting to get in the way. for some reason, my colliders are not working when I have ignore raycast on.?
     
  20. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    When a collider is disabled, it is no longer detected by Raycasts.

    Post your code.
     
  21. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    How does code have to do with this?
    Which code? my collider code or my raycast code? Heres uh.. some of it.

    Unfortunately, with the new unity forum setup, I cannot find the code tags. :(

    So uh please cope.


    Following Mouse:

    juj is a boolean that is true at the start


    if(juj == true){
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit, 100)) {
    // hit.point is the value you're looking for
    transform.position = hit.point + new Vector3(0,Scale_Y_Divided_By_2,0);
    asd = transform.position;
    }
    }


    Putting Down:

    if(Input.GetMouseButtonDown(0)){
    if(Paid == false){
    Money.Cash -= Cost;
    Paid = true;
    }
    transform.position = asd;
    juj = false;
    }
     
  22. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Who needs the button ;)? Just mark it up manually, all you have to do is write: [code_]void DoSomething();[/code] without the underscore of course. It's easy!
     
  23. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Everything.

    Unfortunately I'm having a hard time understanding your code though. It looks like you are only allowing a single placement, is that correct?

    Either way, make sure that the collider on your GameObject is disabled, or disable it on Awake so that the Raycasts won't interact with it.
     
  24. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    A single placement at a time.
    Sorry for not explaining this before, but I have a shop that copies a prefab with this script in it.
     
  25. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    Oh well. Why don't I try that LayerMask thing? How do I do that?
     
  26. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    LayerMask

    The LayerMask route is very similar to the collider route, except instead of enabling/disabling the collider you are changing the layer of the object.