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
1
Question by mmalleske · Feb 25, 2012 at 03:13 AM · cameraobjectattachz-axis

Attaching a Camera to the Player

This is going to make me sound dumb but I am working on a side scroller and when I drag the camera to the player to attach it, the camera's z position is moved to the gameObject. I want to move the camera back off the object, but I can only adjust the x and y axes.

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 syclamoth · Feb 25, 2012 at 03:21 AM 0
Share

You need to provide more details.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jaised · Feb 25, 2012 at 06:00 AM

I am not sure why you would want to attach the camera to the player. Is there something specific that you want to do with it? Because an easier way would be to attach a custom script much like the following:

 #pragma strict
 
 ///    
 ///    SmoothFollow:
 ///        Follows the target's position while applying damping to prevent jittering
 ///        and immediate moving of the camera.This way every subtle movement doesn't become
 ///        noticeable. The camera follows from a set distance away from the character and always
 ///        tries to center the view on the target.
 ///

 // public fields
 var target : Transform;                    // the target camera is following
 var dampTime : float = 0.3;             // offset from the viewport center to fix damping
 var distance : float = 10;                // distance away on z axis from the target
 
 // private fields
 private var _velocity = Vector3.zero;    // velocity of camera relative to target
 
 /// 
 ///    Update:
 ///        Makes sure that the camera is always focused on the target, 
 ///        after applying damping.
 ///
 function Update() {
     if(target) {
         // calculate the difference between camera and target positions
         var delta : Vector3 = target.position - camera.ViewportToWorldPoint(Vector3(0.5, 0.5, distance));
         
         // calculate destination of the camera based on the difference in positions
         var destination : Vector3 = transform.position + delta;
         
         // apply the new position through dampening to provide smooth movement
         transform.position = Vector3.SmoothDamp(transform.position, destination, _velocity, dampTime);
     }
 } // eo Update


Then all you have to do is attach this script to your camera, and link your player Game Object to target and BAM! Good luck.

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
1

Answer by Kleptomaniac · Feb 25, 2012 at 10:27 AM

So I think what you're saying is that the camera is not giving you the scene depth you want? An approach to this could be to add a script to the camera called SideScrollCamPos:

 var player : GameObject;
 var depthZ : float; //Here you will want to specify the z distance of the camera from the character
 
 function Update () {
 transform.position = Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z - depthZ);
 }

I think that should work, but with me there's always an element of being completely wrong ...

Good luck! Klep

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 cosmopod · Dec 24, 2013 at 08:17 PM

It's better if you use LateUpdate() for this purpose instead Update()

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

9 People are following this question.

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

Related Questions

Attaching an object to a camera movement weird, Help! 2 Answers

How can I change camera when colliding with "X" object? 1 Answer

How to set the position of objects on the left and right of the screen 0 Answers

Typecasting Camera to Object for ObjectField 3 Answers

How to attach objects using a mouse click on one side, and object only attaches to that one side? 0 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