• 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 /
  • Help Room /
avatar image
0
Question by Polly_Barlow · Feb 05, 2020 at 03:48 AM · rotatefollowthird-person-cameramousewheel

Third person camera : follow and mouse wheel rotation

Hello,

I'm a beginner with C# and I try to make a Third Person camera that follow my character but also rotate around him when I click on Mouse Wheel.

Problem: I can't find a solution to do both.

This is the first version of my code. With this one, the camera follow my character and rotate, but with mouse mouvement (and i really don't like it)

 public class PlayerCamera : MonoBehaviour {
 
   public GameObject player;
   private Vector3 offset;
 
 
   [SerializeField] int rotationSpeed = 3;
   [SerializeField] bool rotationCam = true;
 
   void Start()
   {
     offset = transform.position - player.transform.position;
   }
 
 
   private void LateUpdate()
   {
     transform.position = player.transform.position + offset;
 
           if(rotationCam)
           {
             Quaternion turnAngle = Quaternion.AngleAxis(Input.GetAxis("Mouse X")
             * rotationSpeed , Vector3.up);
             offset = turnAngle * offset;
           }
 
           transform.LookAt(player.transform);
 
   }
 
 }

And I find this one somewhere here. This one is perfect to rotate camera with mouse wheel, but don't follow my character.

  using UnityEngine;
  
  public class StreetViewCamera:MonoBehaviour {
      public float speed = 3.5f;
      private float X;
      private float Y;
  
      void Update() {
          if(Input.GetMouseButton(0)) {
              transform.Rotate(new Vector3(Input.GetAxis("Mouse Y") * speed, -Input.GetAxis("Mouse X") * speed, 0));
              X = transform.rotation.eulerAngles.x;
              Y = transform.rotation.eulerAngles.y;
              transform.rotation = Quaternion.Euler(X, Y, 0);
          }
      }
  }


Any help and/or suggestions on where to find some information to do both, would be much apprecieated :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by tormentoarmagedoom · Feb 05, 2020 at 11:59 AM

Hello there.

If the second code is working as you want for the rotation, you just need to add the "follow" part. This means change the transform.position of the camera according to character position. Whic is this line in the 1st code:

      transform.position = player.transform.position + offset;

But, I don't know how is the camera configured in the hicheracy, i mean, if isa child of some other emptyobject, or child of the character, etc... You should try first.

A "trick" i do for this type of cameras, if to have an empty object (lets call it CameraBox) that always follow the character, is always at same coords as the character (center of the box is always the character). Then, this CameraBox have the camera as a child. The camera have some offset (from the center of the CameraBox) and is pointing the center of the box (also the character) so, everytime the box rotate, the camera is still pointing the character preserving the offset.

So you move and rotate the box, not the camera itself.

Good luck! Bye!

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 Polly_Barlow · Feb 05, 2020 at 04:23 PM

Wow, I'll never think it was that simple ! That works, thank you so much @tormentoarmagedoom !

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

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

Turret following target 0 Answers

Make Enemy Shoot At Specific Location? 0 Answers

Make a cube follow your finger iOS C# 2 Answers

Model wont follow gun 1 Answer

Click a GUI button to rotate an object 2 Answers

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