Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 stringHakunaMatata · Mar 31, 2021 at 08:16 PM · camera-movementtargetcamera follow

how the camera will follow the rotation in one axis only?

Hi. So here is my code. alt text

So what I am trying to do is make camera follow my player on x and z axis. And at the same time follow rotation of player on y axis only.

I have tried to do it myself but I guess I am missing something as always. Please be sure to explain the answer as I am just a student. Thx a lot

Hi bro @DrTomato

capturejpg-2.jpg (45.9 kB)
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 highpockets · Apr 01, 2021 at 11:22 AM

Ok, so generally the way that I approach this scenario is to add another level of control which is another game object which is right at the players position and it follows the player wherever it goes and rotates on just the y axis when the player rotates around the y axis, but does not rotate around any other axis. This is just an empty object which we can call "CameraTarget". Do not make this object a child of the player, but make the camera a child of the "CameraTarget" object and place the camera at the offset you want it in the scene view before the game starts so the local position is predetermined (no need to add an offset in the script). Now instead of your Camera following the player, it is following this invisible object at the player's origin. So you need to track the player like so:


 public class CameraFollow : MonoBehaviour
 {
     public Transform camTarget;
     public Transform player;
 
     private void LateUpdate()
     {
         camTarget.position = player.position;
         //rotate to face the camera target to face the same forward direction as the player
         camTarget.rotation = Quaternion.LookRotation(player.forward, Vector3.up);
     }
 }



This is fairly basic, but it is a start. From here you may want to dampen the movement of the camera and so instead of the camera as a child of the Camera Target, you make another empty object a child of the Camera Target and you place it where the normal camera with the offset should be and then have this script above on this new object and make a smooth damp script for the camera to follow this new object and include a look at function so the camera stays locked onto the player pos..

EDIT: I edited the code, little bug.

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 stringHakunaMatata · Apr 03, 2021 at 03:41 PM 0
Share

Hi.. so it did not actually worked out for me. Here is what I did, I made a new gameobject(cameraTarget) and set it to my player's position. Then I made my camera child of cameraTarget. Then I applied your script (camfollow2) and rigidbody to cameraTarget. By using rigidbody I froze it's rotation on x and z axis.

Here is everything for better understanding. alt text

alt text

n1.jpg (11.6 kB)
n3.jpg (31.2 kB)
avatar image highpockets stringHakunaMatata · Apr 03, 2021 at 10:45 PM 0
Share

Ok, not sure what didn’t work out for you. A rigidbody seems a little overkill for what you’re trying to accomplish, but as long as you got it working. A rigidbody is meant to be used with the physics engine FYI

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

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

Related Questions

Top down camera lock 2 Answers

Make the camera switch offset on x axis with the character direction 0 Answers

Unity 2D Fighting Game Camera 0 Answers

How to Keep the Camera Centered and Moving When the Player Is off to the Side?,How to Keep the Camera Centered While Moving the Player? 0 Answers

Can you offset the dead zone of Cinemachine's camera from the center? 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