- Home /
ScreenToWorldPoint question
 Vector3 v3Pos = new Vector3(Input.mousePosition.x, 10, Input.mousePosition.y);
 v3Pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y-30,10));
 a.rigidbody.position = new Vector3(v3Pos.x, v3Pos.y-30, v3Pos.z);
I'm trying to make a game where the camera looks down on a flat plane and the player moves the mouse(thus moving a pen) to draw. I've tried a number of ways to get the pen to follow my mouse, but then have them have worked. With my current code the pen can only be moved through a small area in the center of the screen. I need to be able to move my pen anywhere on the screen without it flying to the very edge of the game world. Any advice? Also, for clarification. I only want the pen to move in the x and z position. I want it to remain locked in its set y position.
Answer by robertbu · May 11, 2014 at 05:55 AM
Try this:
 using UnityEngine;
 
 public class Bug25b : MonoBehaviour {
 
     void Update() {
         float dist = Camera.main.transform.position.y - transform.position.y;
         Vector3 v3Pos = Input.mousePosition;
         v3Pos.z = dist;
         transform.position = Camera.main.ScreenToWorldPoint (v3Pos);
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Making the position of the mouse cursor, the reference point for forward movement (2D) 1 Answer
Player wont follow touch controls? 1 Answer
Mouse position for player movement 0 Answers
Camera.ScreenToWorldPoint with Perspective camera 1 Answer
Sprite with non-constant jitter during constant velocity movement. 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                