I am new to Unity and I have a script that changes camera angle to a new position on a click of a button. I want to add the ability to drag and drop various objects from these multiple camera angles. The meshes and colliders on the object and its children are all setup.
This is the code for switching the camera on the button click:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchCameraPosition : MonoBehaviour
{
public Transform cameraTarget1;
public Transform cameraTarget2;
public Transform cameraTarget3;
public Transform cameraTarget4;
public float sSpeed = 10.0f;
public Vector3 dist;
public Transform lookTarget;
private int currenttarget;
private Transform cameraTarget;
// Start is called before the first frame update
void Start()
{
currenttarget = 1;
SetCameraTarget(currenttarget);
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
Vector3 dPos = cameraTarget.position + dist;
Vector3 sPos = Vector3.Lerp(transform.position, dPos, sSpeed * Time.deltaTime);
transform.position = sPos;
transform.LookAt(lookTarget.position);
//Camera.main.transform.position = Vector3.Lerp(transform.position, dPos, sSpeed * Time.deltaTime);
}
public void SetCameraTarget(int num)
{
switch (num)
{
case 1:
cameraTarget = cameraTarget1.transform;
break;
case 2:
cameraTarget = cameraTarget2.transform;
break;
case 3:
cameraTarget = cameraTarget3.transform;
break;
case 4:
cameraTarget = cameraTarget4.transform;
break;
}
}
public void SwitchCamera()
{
if (currenttarget < 4)
currenttarget++;
else
currenttarget = 1;
SetCameraTarget(currenttarget);
}
}
This is the code for the drag and drop script that I have so far although it doesn’t work even on the main camera. It was working without the different camera angles. How would i go about making the script function with different locations?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragObject : MonoBehaviour
{
private Vector3 mOffset;
private float mZCoord;
void OnMouseDown()
{
mZCoord = Camera.main.WorldToScreenPoint(
gameObject.transform.position).z;
// Store offset = gameobject world pos - mouse world pos
mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
}
private Vector3 GetMouseAsWorldPoint()
{
// Pixel coordinates of mouse (x,y)
Vector3 mousePoint = Input.mousePosition;
// z coordinate of game object on screen
mousePoint.z = mZCoord;
// Convert it to world points
return Camera.main.ScreenToWorldPoint(mousePoint);
}
void OnMouseDrag()
{
transform.position = GetMouseAsWorldPoint() + mOffset;
}
}