Search Unity

Question How to use screen to world when using cinemachine?

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

  1. TheKrazyDev

    TheKrazyDev

    Joined:
    Sep 8, 2021
    Posts:
    107
    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

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    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

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    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?