Gaining the Z value in ScreenToWorldPoint().. newbie here,How do i get the z distance in ScreenToWorldPoint().. noob here
Hello, I am learning and just starting out in unity and C#. I have a basic understanding and knowledge of programming, and I am trying out a few ideas whilst following along some great courses on unity learn.. I am trying to get an object to follow my mouse cursor. Im in 3D.
public class MousePlayerController : MonoBehaviour
{
public float speed = 0.2f;
private Vector3 mousePos;
private Vector3 directionFromPlayer;
private Camera cam;
// Start is called before the first frame update
void Start()
{
cam = Camera.main;
}
// Update is called once per frame
void Update()
{
mousePos = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cam.nearClipPlane));
transform.Translate(Vector3.MoveTowards(transform.position, mousePos, speed * Time.deltaTime));
//transform.Translate(Vector3.right * speed * Time.deltaTime);
}
}
When I press play I get the object just launching in the Y direction. I have tried using Input.mousePosition but have found out that is only returning x, y coordinates in 2d space.. I have never done raycasting or know where to start and have been given that way to do what i am after also. Could someone please take the time to explain to me in laymans terms to get this to do what I need to do... Thanks
Your answer
Follow this Question
Related Questions
Can someone tell me how this code works? 1 Answer
Mouse Look Script Not Working 1 Answer
New Input System first person camera stutter. 0 Answers
drag AND drop push and pull object 0 Answers
Help with Inverse Mouse Panning (Middle mouse drag 0 Answers