• 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 dkasanders00 · Jan 05, 2018 at 09:38 PM · camerarotationcamera-movementcamera rotate

How to make the camera follow the player while still being able to be rotated?

In a camera script, I want the camera to be able to rotate around the player on the y-axis. This works fine, but when I make the camera follow the player, it won't be able to rotate because it's constantly being set to a certain position. How can I make the camera follow the player, but still rotate around the player as well? using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class CameraScript : MonoBehaviour {
     public Transform target; 
 
     public Vector3 offset;
 
     public float yawSpeed = 100f;
 
     private float yawInput = 0f;
     private bool mouseDown = false;
     void LateUpdate()
     {
         //transform.position = target.position + offset;
         transform.LookAt(target);
         if (Input.GetMouseButtonDown(1))
         {
             mouseDown = true; 
            
         }
         if(Input.GetMouseButtonUp(1))
         {
             mouseDown = false;
         }
         if (mouseDown)
         {
             yawInput = Input.GetAxis("Mouse X") * yawSpeed * Time.deltaTime;
 
             transform.RotateAround(target.position, Vector3.up, yawInput);
 
             
 
            
             
             target.gameObject.transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
          
             
 
             print(target.gameObject.transform.rotation.y);
 
            
         }
      
 
     }
 }

Thanks!

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 black_sheep · Jan 06, 2018 at 09:34 AM

I suppose by the Y axis you mean to rotate AROUND the player, not going up and down, neither getting closer or further.

I'll do it by having a transform poiting to the the angle we are looking from the player (0 is we're facing him, 90 is besides, 180 is on the back), than going forward, making at look at the player, than making it the camera transform. I didn't test the code but i think it Works. Anyway i made my point.

     using UnityEngine;
     
     public class CameraTrack : MonoBehaviour {
 
     public float rotateSpeed=4.0f;    //Time in seconds it takes for the camera to rotate around the Player
     public float distance=4.0f;    //Distance from the camera to the player
 
     Transform player;    //Make the camera a child object of the player in the inspector
 
     Transform auxT;
     float currentRot;
 
     void Start(){
 
         player=transform.root;
 
     }
 
     void Update(){

             currentRot += (input.GetAxis("Mouse X") * (360/(rotateSpeed*time.deltaTime));
             currentRot %= 360;

             auxT = player;
             auxT.rotation.y = currentRot;
             auxT.position = Vector3.forward * distance;
             auxT.lookAt(Player);
 
         }
 
         transform = auxT;
 
 }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

137 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 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

Why is my camera not snapping 90 degrees? 0 Answers

Rotating the camera over a period of time 1 Answer

Forward and back movements with a camera emulating an isometric view 1 Answer

Camera Rotation 1 Answer

Camera shaking when moving around motionless objects 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges