Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Kitty · Jan 26, 2010 at 04:05 PM · cameraraycastscreen

Camera Point-and-click Movement

I'm a noob at scripting and haven't seen what I would like to be able to do. Or maybe I have and it hasn't dawned on me....

The idea is to use a script applied to many different cubes that allows a cube to be clicked. When clicked the camera should snap to a location directly in front of that cube. When clicked again, the camera should snap back to its initial location.

I've figured out how how to get a screen cross fade to work between 2 cameras; however, I need to be able to use just the main camera. Any help would be greatly appreciated.

Thanks.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Brian-Kehrer · Jan 26, 2010 at 04:24 PM

This is the bit of code I think you want: EDIT: Hacked to solve your comment.

var myCamera : Transform; var targetPosition : Vector3; var zoomedIn : boolean; var defaultPosition : Vector3;

function Update () { if(Input.GetMouseButtonDown(0)){ if(zoomedIn){ targetPosition = defaultPosition; zoomedIn = false; } else{

         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         if (Physics.Raycast (ray,hit, 50)) {
             Debug.Log(hit.point);
             targetPosition = hit.point;
             zoomedIn =true;
         }
     }
 }

 myCamera.position += 0.25*(targetPosition-myCamera.position);

}

From RaycastHit, you can detect which object you have hit, and also get the point of the hit, allowing you to move the camera.

Comment
Add comment · Show 5 · 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
avatar image Kitty · Jan 26, 2010 at 05:43 PM 0
Share

I thought that might work; however, it's not quite right.

I've got the project designed layed out kinda like the periodic table, so I'm trying to make it more of a learning activity program more than a ga$$anonymous$$g. The user should be allowed to click on an object and the camera move in to see certain information then be allowed to click on it again to see a full view of the layout.

Since there's more than one box of information, I remember seeing something about interpolation for the position and direction of the movement. $$anonymous$$aybe something along there would be the best approach?

avatar image Brian-Kehrer · Jan 26, 2010 at 06:09 PM 0
Share

This just gets you the correct position or object that the user clicked on - interpolation would be different, ill post a quick sample

avatar image Kitty · Jan 27, 2010 at 03:31 PM 0
Share

Be awesome if you could do so.

avatar image Melissa · Feb 07, 2010 at 04:18 PM 0
Share

I am also trying to do something similar, but I'm having difficulty precisely controlling the camera movement. It's shaky in parts. I want the user to click on certain objects in 3D space and be "zip lined" to that location. It would be A$$anonymous$$AZING if you could post a sample. Thanks!

avatar image Kitty · Feb 22, 2010 at 04:33 PM 0
Share

I'm like $$anonymous$$elissa. I like how it goes to where you click; however, I'm looking for something that'll make it go to the center of that object despite where the user clicks. I've put this project aside to work on other things, so I haven't had time to figure it out. Hopefully I'll pick back up on it later this week.

avatar image
1
Best Answer

Answer by Kitty · Mar 05, 2010 at 02:53 PM

I had help in another one of my questions, but here's the code that I'm working with applied to the camera that will offset from the object clicked.

#pragma strict

var myCamera : Transform; var targetPosition : Vector3; var zoomedIn : boolean; var defaultPosition : Vector3; var camOffset : Vector3;

function Start(){ targetPosition = myCamera.position; }

function Update () { if(Input.GetMouseButtonDown(0)){ if(zoomedIn){ targetPosition = defaultPosition; zoomedIn = false; }
else{ var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray,hit, 50)) { //targetPosition = hit.point; var hitPosition = hit.transform.position; targetPosition = hitPosition+camOffset; zoomedIn =true; }
}
}
myCamera.position += 0.25*(targetPosition-myCamera.position); }

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

No one has followed this question yet.

Related Questions

How to engage an object if it's seen by my camera?? 1 Answer

Question about sending physics raycast from the camera 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Logic to detect objects entering between the player and camera? 1 Answer

Jerky Camera and Varying Translate speeds 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