Answer the question
In order to leave comments, you need to log in
How to solve the problem with the camera in 2d?
everything works on the map with the camera using RMB and the object moves with the help of AWSD (in the wrong direction, but not the point). It is necessary to make it so that by clicking LMB on the object, the camera is attached to the object and move the object with the camera, and using the button to disconnect the camera .. all this is done normally. But some little things can not be fixed, this. If you move the object with the camera and detach it, then the camera returns to its old place, if you move the object further from the initial position and click the LMB, then the camera is attached xs where . as a result, the camera and the object seem to have different speeds chtoli and the coordinates seem to be initial in the center and everything revolves around them, you move the object very far, then it’s a mess in general .. it is necessary that it attaches normally everywhere and detaches at the place of the object, and does not fly away to the beginning. Maybe it's a 3d problem?
two codes (hehe):
using UnityEngine;
public class CameraController : MonoBehaviour
{
public static CameraController instance;
public Transform followTransform;
public Transform cameraTransform;
public float movementTime;
public Vector3 newPosition;
public Vector3 dragStartPosition;
public Vector3 dragCurrentPosition;
void Start()
{
instance = this;
newPosition = transform.position;
}
void Update()
{
if (followTransform != null)
{
transform.position = followTransform.transform.position + newPosition;
}
else
{
HandleMouseInput();
}
if (Input.GetKeyDown(KeyCode.G))
{
followTransform = null;
}
}
void HandleMouseInput()
{
if (Input.GetMouseButtonDown(1))
{
Plane plane = new Plane(Vector3.forward, Vector3.right);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float entry;
if(plane.Raycast(ray, out entry))
{
dragStartPosition = ray.GetPoint(entry);
}
}
if (Input.GetMouseButton(1))
{
Plane plane = new Plane(Vector3.forward, Vector3.right);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float entry;
if (plane.Raycast(ray, out entry))
{
dragCurrentPosition = ray.GetPoint(entry);
newPosition = transform.position + dragStartPosition - dragCurrentPosition;
}
}
transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementTime);
}
}
using UnityEngine;
public class Motorcycle : MonoBehaviour
{
public void OnMouseDown()
{
CameraController.instance.followTransform = transform;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question