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

Question How to use screen to world when using cinemachine?

Discussion in 'Scripting' started by TheKrazyDev, Jun 13, 2022.

  1. TheKrazyDev


    Sep 8, 2021
    Hello. So I have a script where the player has a sword which points towards the mouse pointer, but I'm using cinemachine for my camera ( Its a 2dCamera) so for some reason when I start moving away from the 0 on the x or y the sword gets all jacked up and doesnt point towards the mouse pointer. Is there a alternate thing of ScreenToWorld for cinemachine? Sorry if this shouldn't be in the scripting section.
  2. karliss_coldwild


    Oct 1, 2020
    Most likely the problem isn't in screenToWorld but in your code mixing up vectors representing absolute positions (in some coordinate system) and relative positions meaning direction+distance.

    In general Cinemachine mostly manipulates the parameters for existing normal Unity cameras. So most of the unity camera API is still relevant when using Cinemachine, except you shouldn't try to modify values that Cinemachine controls because in that case your code will be fighting Cinemachine over them.

    Next time you are asking a question in forum try to provide slightly more information of what exactly you did and what happens, include the relevant parts of your code. It is hard to help anyone if all they say is that they they tried to do X and they failed to do it properly so it didn't work. That forces any potential help to guess which of the hundred different ways for making a mistake did you do, this wastes time of everyone involved including you.
  3. Gregoryl


    Unity Technologies

    Dec 22, 2016
    Yes, Cinemachine just moves the main camera around, so you can still call camera.ScreenToWorld. One gotcha that people often trip over: Cinemachine moves the camera very late in the frame, after most other scripts have run. That means that if you're doing ScreenToWorld in the normal update loop, you will be using the camera position from the previous frame. That can cause the results to be off if the camera is moving. You must be sure to do ScreenToWorld after CinemachineBrain.LateUpdate has executed. Could this be causing your issue?