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 xjimdim · Aug 28, 2014 at 02:53 AM · cameraspacecenter

How can i center the camera to a moving object using space?

Hello there, I've been trying to create a MOBA kind of game (like DOTA, LoL etc) and I want to create a camera that when you push space it centers to your character (and if you do not release space it continues to center to your character even if it moves). So i tried this code in the Update function:

 var myplaya:GameObject;
 myplaya = GameObject.Find("MyPlayer");
  
 if(Input.GetKey(KeyCode.Space)){
         transform.position.x = myplaya.transform.position.x;
         transform.position.z = myplaya.transform.position.z; 
         transform.position.y = 91f; // this is always static i just have it there for when i impliment zoom
         

it works like I want the only problem is it does center to the character, it centers to another place in the map (however if o move the caracter it moves the same way).

I think I have to add the angle of the camera (x rotation of the camera gameobject) to the equation but i cant figure out how.

Any help?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AlwaysSunny · Aug 28, 2014 at 03:21 AM

The most straightforward, flexible solution is creating a camera rig. Create a gameobject at the world origin. Make your camera a child of that gameobject. Reposition the camera to achieve the distance / angle / focus you want. Your code should move the camera's parent. This allows you to have whatever camera angle / orientation you want while maintaining simple movement logic.

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 xjimdim · Aug 28, 2014 at 07:37 PM 0
Share

Hey!! thnx for the answer!!

I tried your advice and it kinda works.

The problem now is that the camera rotates when my gameobject rotates. What I mean is that the camera now always focuses to one site of my gameobject no matter if i move it up or down and this way you can see the map from a total new side everytime you move (it is not locked from left to right, it goes everywhere). If you have played League of Legends you can understand what i mean (pressing space will center the camera to your champion and if you click to go down the champion goes down and you can see his face or something, if you click up you see his back etc). $$anonymous$$y code at the first post almost did the trick but it just didnt focus at the right place (the camera movement was great however).

Hope I didnt confuse you :P

avatar image AlwaysSunny · Aug 29, 2014 at 12:21 AM 0
Share

Are you making the camera rig a child of the player? That's not the intended usage of a camera rig. A typical camera rig should either share or seek the position of the VIP your camera wants to view. In this case, the player.

Its child - which is the actual camera - can have any behavior you like, including no behavior whatsoever, which sounds like what you want. Just means you shouldn't include any code which affects the rotation of the camera or camera rig, and the camera rig shouldn't be a child of anything which can move or rotate.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Camera setup question 2 Answers

Camera Orbit for spaceship 1 Answer

Screen Space is non standard in Unity? 1 Answer

How to make camera position relative to a specific target. 1 Answer

How take item if I am bringing rear sight and press key E? 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