- Home /
move camera based on mouse click on object
Hello,
I am working on a simple puzzle game but I am horrible with programming. I looked around and was able to figure out a few things about moving the camera but I am having issues with using Raycasting to move the camera when I click on an object.
Here is what I got so far. This script is attached to the object to be clicked on:
Vector3 moveToPosition; // This is where the camera will move after the start
Vector3 backToPosition; // This is where the camera will move if not in the original position
float speed = 2f; // this is the speed at which the camera moves
bool cameraMoved = false; // this checks to see if the camera was moved
// Start is called before the first frame update
void Start()
{
//Move the camera to this position
moveToPosition = new Vector3(100, 0, -10);
//Move the camera back to the starting position
backToPosition = new Vector3(0, 0, -10);
}
// Update is called once per frame
void Update()
{
//Mouse Click on Cube 1
if (Input.GetMouseButtonDown(0))
{
//Create variables to cast a ray on Cube 1
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//Detect if ray hit Cube 1
if (Physics.Raycast(ray, out hit, 100))
{
//If camera was not moved...
if (!cameraMoved)
{
// Move the camera into position
transform.position = Vector3.Lerp(transform.position, moveToPosition, speed);
cameraMoved = true;
}
//If camera was moved...
else
{
// Move the camera back into the starting position
transform.position = Vector3.Lerp(transform.position, backToPosition, speed);
cameraMoved = false;
}
}
}
}
What am I missing? Thanks!
First, make sure your object has a collider. Secondly, what does removing the maxDistance parameter from line 27 do? I could be wrong but i think if you dont specify the maxDistance it will cast an infinite line. It might be that your ray just is too short to collide.
I don't think that is it, because with the current code, when I click on the cube, it disappears. And when I add this code:
float maxDistance = 100f;
//Detect if ray hit Cube 1
if (Physics.Raycast(ray, out hit, maxDistance))
the cube also disappears. Unless I'm typing in the wrong code. Please help! Thank you!
I'm pretty new to unity but I'll play around to see if i can get it to work. Just so i completely understand the problem, youre trying to move the camera to a different location when you left click on on abject?
Answer by dylan1812 · Jun 18, 2020 at 03:14 PM
Unless I misunderstand the question your Script worked pretty well. This is how I adapted your code
public class CameraMoveOnClick : MonoBehaviour
{
Vector3 startPosition = new Vector3(0, 0, -10); // This is where the camera will start
Vector3 movedPosition = new Vector3(10, 0, -10); // This is where the camera will move when object is clicked
bool cameraMoved = false; // this checks to see if the camera was moved
[SerializeField] private LayerMask objectLayer;
// Start is called before the first frame update
void Start()
{
//Move the camera to starting position
transform.position = startPosition;
}
// Update is called once per frame
void Update()
{
//Mouse Click on Cube 1
if (Input.GetMouseButtonDown(0))
{
//Create variables to cast a ray on Cube 1
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//Detect if ray hit Cube 1
if (Physics.Raycast(ray, out hit, 100, objectLayer))
{
//If camera was not moved...
if (!cameraMoved)
{
// Move the camera into position
transform.position = movedPosition;
cameraMoved = true;
}
//If camera was moved...
else
{
// Move the camera back into the starting position
transform.position = startPosition;
cameraMoved = false;
}
}
}
}
}
when the object with the layer "object" is clicked the camera will move 10 units positive in the x axis, when clicked again the camera will move back. This script needs to be assigned to the camera and not the object and in the inspector you need to add the layer "object" and assign that layer to the object you are clicking.
Your answer

Follow this Question
Related Questions
Can I change GUIButton behaviour? 1 Answer
Convert vector in world to mesh 1 Answer
Remove a part of image on mouse click ? 1 Answer
Activate gameobject upon mouseclick 1 Answer