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 infinitypbr · Jun 17, 2013 at 11:29 PM · cameramovementtop down

Top-Down Camera: Always looking where the player is facing?

I use this code, which I did not write, to move the camera so it's always looking at the player, top-down 3rd person, as they move around the world. I'd like to adjust it so that when the player turns left or right, the camera moves around as well, so that the camera is always facing the same direction as the player.

[I've tried making the camera a child of the player object, but the result is a choppiness as the camera moves right/left with player but then gets pushed back, probably via the code]

How might I adjust this code to get the desired effect? I'm all for doing the research myself, if someone wants to point me in the right direction :)

 using UnityEngine;
 using System.Collections;
 
 public class AttachCamera : MonoBehaviour
 {
     Transform myTransform;
     public Transform target;
     public Vector3 offset = new Vector3(0, 5, -5);
     
     void Start()
     {
         myTransform = this.transform;
     }
     
     void FixedUpdate()
     {
         if (target != null)
         {
             myTransform.position = target.position + offset;
             myTransform.LookAt(target.position, Vector3.up);
         }
     }
 }
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
2
Best Answer

Answer by whebert · Jun 18, 2013 at 12:06 AM

If you want to try the parented approach:

 void Start()
 {
    transform.parent = target;
    transform.localPosition = offset;
    transform.LookAt(target);
 }

If not:

 void Update()
 {
    myTransform.position = target.position + target.TransformDirection(offset);
    myTransform.LookAt(target);
 }
Comment
Add comment · Show 3 · 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 infinitypbr · Jun 18, 2013 at 12:26 AM 0
Share

Awesome -- the 2nd one worked, I wasn't able to get the first to work.

I had not seen TransformDirection before, so thank you for giving me something new to check out!! :)

avatar image whebert · Jun 18, 2013 at 12:36 AM 0
Share

Cool, glad you got it working. Did you try the first one with only the Start, no FixedUpdate or Update for that matter? I didn't test it, but should work. Also, FixedUpdate is mostly for physics type updates, like when moving a rigid body. Update is recommended for smoother transform changes.

avatar image infinitypbr · Jun 18, 2013 at 06:07 AM 0
Share

C# actually isn't my language of choice, which is part of why I was having trouble with it. So I'm guessing that the first one failed because my own lack of knowledge.

However, the 2nd one is quite smooth. I'm wondering, though, if you might have a solution to a follow-up problem?

I've asked the question here: http://answers.unity3d.com/questions/476458/slerping-camera-top-down-view-how-to-keep-it-over.html

If you know of a possible solution, that would be SUPER helpful!!

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

15 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

Related Questions

How to use Android Accelerometer(Left and right tilt) to turn a sphere object and camera in that direction? 1 Answer

Moving forward in 3D 1 Answer

Constrain axis of isometric camera ("vertical" only) 0 Answers

Why is an object's movement jerky, when the camera is also moving by following a different rigidbody? 3 Answers

Move a Game Object (Camera) forward and sideways with the Mobile Sensor Outputs. How? 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