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 Triqy · Mar 16, 2014 at 10:38 PM · camerarotationrotaterelative

Camera follows target rotation

I have a camera script that follows the player and i need the camera's rotation to be relative of the targets rotation. Example: The camera will view the player at a top down angle that is constantly turning with the player. Right now what its doing is the camera is following the player, but when i turn the player using the arrows the camera is not turning with the player.

 using UnityEngine;
 using System.Collections;
 
 public class CameraFollow : MonoBehaviour {
 
     public Transform target;
     public float smoothTime = 0.1f;
     public float speed = 3.0f;
     
     public float followDistance = 10f;
     public float verticalBuffer = 1.5f;
     public float horizontalBuffer = 0f;
     
     private Vector3 velocity = Vector3.zero;
     
     public Quaternion rotation = Quaternion.identity;
     
     void Update () {
         Vector3 targetPosition = target.TransformPoint(new Vector3(horizontalBuffer, followDistance, verticalBuffer));
         transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
     
         rotation.eulerAngles = new Vector3(90, target.localRotation.y, target.localRotation.z);
         rotation.y = target.rotation.y;
         
 }
 }
 
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 robertbu · Mar 16, 2014 at 10:53 PM 0
Share

In order for the camera to turn with the player, the camera will need to rotate around the player. Take a look at the standard $$anonymous$$ouseOrbit script to get some ideas on how this is done.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by blenderblender · Dec 15, 2014 at 12:30 PM

 using UnityEngine;
 using System.Collections;
 
 public class TransformToPlayer : MonoBehaviour {
     
     public Transform target;
 
     public float speed;
     void Update() {
     
 transform.eulerAngles = new Vector3(0, target.transform.eulerAngles.y, 0);
     }
     }
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 Triqy · Mar 16, 2014 at 10:59 PM

I figured it out thank you.

 using UnityEngine;
 using System.Collections;
 
 public class CameraFollow : MonoBehaviour {
 
     public Transform target;
     public float smoothTime = 0.1f;
     public float speed = 3.0f;
     
     public float followDistance = 10f;
     public float verticalBuffer = 1.5f;
     public float horizontalBuffer = 0f;
     
     private Vector3 velocity = Vector3.zero;
     
     public Quaternion rotation = Quaternion.identity;
     
     public float yRotation = 0.0f;
     
     void Update () {
         Vector3 targetPosition = target.TransformPoint(new Vector3(horizontalBuffer, followDistance, verticalBuffer));
         transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);

         //this is the code that solves the problem
         transform.eulerAngles = new Vector3(90, target.transform.eulerAngles.y, 0);
         //------------------------------------------------------------------------
 }
 }
 
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 robcbryant · Mar 16, 2014 at 11:53 PM

I don't have time to test it out but you can try this:

 YourCamera.transform.rotation = PlayerObject.transform.rotation;

Put that in the update loop and see if it works--I think it should work as you envision as long as the player is ONLY rotating on the x/y axis--one issue with this might be that it copies the initial z rotation of the player object--but it shouldn't change after that. it's worked for me in 2d projects--I'm not sure how well for 3d.

You could also try this:

 float newRotation = PlayerObject.transform.eulerAngles.y;
 float oldRotation = CameraObject.transform.eluerAngles.y;
 
 YourCamera.transform.Rotate(0, newRotation - oldRotation, 0);


You should only need to rotate the camera on one axis--it should be your y or x axis. Try using the y axis first and then the x. I would also initialize the floats outside the update loop and define them in the loop--but this is a quick dirty test.

Cheers, Gustav

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

22 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

Related Questions

Rotate object relative to camera's view 2 Answers

3D effect with new UI system 1 Answer

Rotate camera only in 2 directions based on player touch 1 Answer

Relative Movement Problem 1 Answer

Rotating a plane so that its top is parallel to the camera viewport 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