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

Cloning mouse speed and vector

Discussion in 'Scripting' started by LaneFox, Jun 28, 2014.

  1. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    Hey there

    I'm trying to find a way to clone the actual mouse speed and direction vector from screenspace and apply it to an object in world space. I need to make an object in worldspace do what the mouse does. I don't know what code to use to get the mouse information. I'm assuming I can get this, then just apply it directly and have it work.

    I thought about a few ways to go about doing it but ideally I would like to just get the speed and direction vector and apply that to a the fake mouse in 3d space that the player is controlling. This is for the Rift, so I don't see any way to allow the use of screenspace. If we do, then we are translating on top of the head rotations which makes the mouse follow the head movements and that just doesn't work at all and is where I'm already at, with a working 3d mouse that follows the head rotation.

    Ideas?
     
  2. sedativechunk

    sedativechunk

    Joined:
    Jan 14, 2014
    Posts:
    35
    I don't quiet understand your question, but if you are trying to get the mouse coordinates in 3D space, then what you want to use it "Camera.ScreenWorldToPoint":
    http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

    Instead of passing a Vector 3 like the documentation demonstrates, you can actually use a Vector2 containing the mouse X and Y coordinates. Those then can be used in 3D space with the "screenworldtopoint" function. Look for some documentation related to the "screenworldtopoint" and you should find some answers as to what you are looking for.
     
  3. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    You need to store the mouse position to variable at the end of Update for example, and then at the begining of Update use prevMousePos - mousePos to get direction, and divide the distance with deltatime to get the velocity.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    I've already got that going, but what happens is when you turn the Rift and look around then screenspace follows it, changing your world coordinate results... So moving your head will move the 3d mouse cursor and it doesn't feel natural.

    This is more along the lines of what I'm thinking will work but the implementation is still hazy. Do you know of any examples? I suppose I can just tinker with it until it starts doing something I want.
     
  5. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    Btw, you can probably use Input.GetAxis("Mouse X") to get what you are after. Something to start with:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class mouseMoveToObjectLocal : MonoBehaviour {
    5.    
    6.     void Update () {
    7.         Vector3 newPos = transform.localPosition;
    8.  
    9.         newPos.x += Input.GetAxis("Mouse X") / Time.deltaTime /10000;
    10.         newPos.x = Mathf.Clamp(newPos.x, -0.42f,0.42f);
    11.  
    12.         newPos.y += Input.GetAxis("Mouse Y") / Time.deltaTime /10000;
    13.         newPos.y = Mathf.Clamp(newPos.y, -0.21f,0.21f);
    14.  
    15.         transform.localPosition = newPos;
    16.     }
    17. }
    If you add this to Cube (your ingame mousepointer), and put the cube to empty gameobject (your ingame screen), you can now rotate the "screen" and pointer still follows correctly until the edge ( see Mathf.Clamp part )