Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by comixian · Jun 18, 2020 at 12:55 AM · mouseclick

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dylan1812 · Jun 18, 2020 at 01:41 AM 0
Share

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.

avatar image comixian dylan1812 · Jun 18, 2020 at 02:34 PM 0
Share

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!

avatar image dylan1812 comixian · Jun 18, 2020 at 02:50 PM 0
Share

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?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

mouse Down+Hold and double click events 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges