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 · Mar 03, 2010 at 06:55 PM · cameragameobject

Center Camera view on Center of GameObject [solved]

I'm trying to figure out what I need to write in order to get the camera to center on a gameobject (this case a cube) after the user as clicked on it. I have a script that works close to what I need;however, it goes to the mouse's location.

I'm not sure what do would be best: to twick the raycast script applied to the main camera, or edit it to act with another script attached to gameobjects. I was thinking if I went with the second, it maybe easier since there is more than one cube in scene.

If someone could help me figure out what to do, that would be awesome. And if I need to explain a little more about the project I'm trying to work on, I can do so.

Here's the raycast script I have.

#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 · Show 1
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 Lipis · Mar 12, 2010 at 11:13 AM 0
Share

@kitty Eventually all the question will be answered and they will be solved. There is no point of including Solved in the title or updating the corrections in the initial question, because the answers will look weird if you can see the initial error for somebody that will encounter similar problem..!

3 Replies

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

Answer by Eric5h5 · Mar 03, 2010 at 07:53 PM

Instead of setting targetPosition to hit.point, try setting it to hit.collider.transform.position.

Comment
Add comment · Show 2 · 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 · Mar 03, 2010 at 08:58 PM 0
Share

Thank you! It works! But any idea why it's running into middle of the cubes and ignoring the box colliders?

avatar image Eric5h5 · Mar 03, 2010 at 09:10 PM 0
Share

You're setting the camera position explicitly, and presumably your camera isn't a rigidbody with a collider, which is the only way that collision events would be generated.

avatar image
0

Answer by David 3 · Apr 07, 2010 at 09:43 AM

Surely the line myCamera.position += 0.25*(targetPosition-myCamera.position); should be within the if(Input.GetMouseButtonDow n test otherwise the script will fire contineously and the camera wont follow commands from things like an fps walker script

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
avatar image
0

Answer by David 3 · Apr 07, 2010 at 09:50 AM

I am not sure if i am using this script correctly - the on click seems to hit any mesh and once clicked i seem to rotate about a point ?!

i have attached the script to a mesh and set the my camera variable to the camera of my fps walker camera.

Comment
Add comment · Show 1 · 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 · Apr 07, 2010 at 02:16 PM 0
Share

I set this script to an empty gameobject with my camera as the main camera and it's functioning the way I need to.$$anonymous$$ain camera zooms and offsets from a gameobject that I have clicked on. Click again, it resets back to the original starting position.

Since that's the case, I'm not sure how it looks for the fps walker camera...

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

Using the accelerometer to rotate Camera 1 Answer

How do you write scripts attached to a game object that move the main camera 1 Answer

Camera to follow a target within a circle? 1 Answer

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

How to run an animation mouse click. 2 Answers


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