Instantiated GameObject that can be dragged does not work while the same GameObject not instantiated works fine
Hi Im currently making a building game in 3D. I programmed a Building script that is on the building. when im dragging the building prefab into the scene the building works perfect. but when im instantiating it the building can be dragged around but when im dragging and moving the camera at the same time the building does not get dragged or it goes in to the oposite direction. It is really weird because it work fine when im dragging it into the scene.
This is the Building script.
public Vector3 Offset;
public float mZCoord;
void Update()
{
Offset = gameObject.transform.position - GetMouseWorldPos();
mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
}
private Vector3 GetMouseWorldPos()
{
Vector3 mousePoint = Input.mousePosition;
mousePoint.z = mZCoord;
return Camera.main.ScreenToWorldPoint(mousePoint);
}
void OnMouseDrag()
{
transform.position = new Vector3(GetMouseWorldPos().x + Offset.x, transform.position.y, GetMouseWorldPos().z + Offset.z);
}
This is the Instantiating script
public GameObject Object;
public void SpawnBuilding()
{
Instantiate(Object, new Vector3(5,0,0), Quaternion.identity);
}
And this is the Camera script
public Transform cameraTransform;
public float fastSpeed;
public float normalSpeed;
public float movementSpeed;
public float movementTime;
public float rotationAmount;
public Vector3 zoomAmount;
public Vector3 newZoom;
public Vector3 newPosition;
public Quaternion newRotation;
public Vector3 rotateStartPosition;
public Vector3 rotateCurrentPosition;
public float minZoom;
public float maxZoom;
// Start is called before the first frame update
void Start()
{
newPosition = transform.position;
newRotation = transform.rotation;
newZoom = cameraTransform.localPosition;
}
// Update is called once per frame
void Update()
{
HandleMovementInput();
HandleMouseInput();
}
void HandleMouseInput()
{
if(Input.mouseScrollDelta.y!=0)
{
if(Input.mouseScrollDelta.y>0 && newZoom.z < maxZoom)
{
newZoom += Input.mouseScrollDelta.y * zoomAmount;
}
if (Input.mouseScrollDelta.y < 0 && newZoom.z > minZoom)
{
newZoom += Input.mouseScrollDelta.y * zoomAmount;
}
}
if (Input.GetMouseButtonDown(1))
{
rotateStartPosition = Input.mousePosition;
}
if(Input.GetMouseButton(1))
{
rotateCurrentPosition = Input.mousePosition;
Vector3 difference = rotateStartPosition - rotateCurrentPosition;
rotateStartPosition = rotateCurrentPosition;
newRotation *= Quaternion.Euler(Vector3.up * (difference.x / 10f));
}
}
void HandleMovementInput()
{
if (Input.GetKey(KeyCode.LeftShift))
{
movementSpeed = fastSpeed;
}
else
{
movementSpeed = normalSpeed;
}
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
newPosition += (transform.forward * movementSpeed);
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
newPosition += (transform.forward * -movementSpeed);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
newPosition += (transform.right * movementSpeed);
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
newPosition += (transform.right * -movementSpeed);
}
transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementTime);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * movementTime);
cameraTransform.localPosition = Vector3.Lerp(cameraTransform.localPosition, newZoom, Time.deltaTime * movementTime);
}
I hope my problem is understandable. Thanks in advance.
Answer by Dario321 · Aug 24, 2021 at 04:53 PM
I tried to use rb movement on the camera and then it also works fine but why is it working with lerp when i drag it into the scene the problem keeps getting weirder