Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
Weird movement when using WorldToScreenPoint with a moving camera

I have a gameobject that moves forward constantly, and I'm controlling it with the mouse on the X axis using the script below, and the camera is following this object.

The issue I have is when letting the camera follow it on the X axis, the object's movement become weird (because of the WorldToScreenPoint position change I think), but it works perfectly when freezing the camera's X position.

My question is how can I make the camera follow this object on the X axis without affecting the movement?

if ((Input.GetMouseButtonDown(0) || Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
    {
        screenPoint = Camera.main.WorldToScreenPoint(transform.position);
        offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, screenPoint.y, screenPoint.z));
    }

    if (((Input.GetMouseButton(0) && !Input.GetMouseButtonDown(0)) || ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)) && !(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)))
    {
        Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, screenPoint.y, screenPoint.z);

        Vector3 curPosition = new Vector3((Camera.main.ScreenToWorldPoint(curScreenPoint).x + offset.x), 0, 0);

        float newX = Mathf.Clamp(curPosition.x, -2, 2);

        transform.position = Vector3.Lerp(transform.position, new Vector3(newX, transform.position.y, transform.position.z), 25 * Time.deltaTime);
    }
over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda