Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Mishkawy · Oct 15, 2017 at 10:11 PM · controllercamera rotatemousepositionmouselookcamera-look

How to rotate camera around player when idle, but control player rotations when moving?

Hello,

I am new to Unity and to scripting in general, I have been trying for a couple of days to achieve this, and after following many tutorials I can now do the following:

1- Control the player using keyboard and player turns around when the camera turns around (mouse X) when player is moving.

2- Player does not turn with camera when idle (and that's what I want).

  • What I am trying to add now is rotate the camera around the player when in idle without rotating the player himself. but continue to follow the camera when the player starts moving again.

Note: I have the main camera attached to the player as a child.

Here is the player control script attached to the player:

 using UnityEngine;
 using System.Collections;
 
 public class PentuControls : MonoBehaviour {
 
         static Animator anim;
         private float speed = 0.1F;
         public float rotationSpeed;
         public float runSpeed;
         public float walkSpeed;
 
 
         // Use this for initialization
         void Start () 
         {
             
                 anim = GetComponent<Animator>();
 
 
             }
         
         // Update is called once per frame
         void Update () 
         {
             
                 float translation = Input.GetAxis ("Vertical") * speed;
                 float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
                 translation *= Time.deltaTime;
                 rotation *= Time.deltaTime;
                 transform.Translate (0, 0, translation);
                 transform.Rotate (0, rotation, 0);
 
                 if (Input.GetKey (KeyCode.LeftShift)) {
 
                         speed = runSpeed;
 
                         //Running
                         if (Input.GetKey(KeyCode.W))
                             {
                                 anim.SetBool ("isWalking", false);
                                 anim.SetBool ("isRunning", true);
                                 anim.SetBool ("isIdle", false);
                                 anim.SetBool ("isIdleLeft", false);
                                 anim.SetBool ("isIdleRight", false);
                             }
 
                         else if (Input.GetKey(KeyCode.S))
                             {
                                 anim.SetBool ("isWalking", false);
                                 anim.SetBool ("isRunning", true);
                                 anim.SetBool ("isIdle", false);
                                 anim.SetBool ("isIdleLeft", false);
                                 anim.SetBool ("isIdleRight", false);
                             }
 
             else if (Input.GetKey(KeyCode.Q) && Input.GetKey(KeyCode.W))
                             {
                                 anim.SetBool ("isWalking", false);
                                 anim.SetBool ("isRunning", true);
                                 anim.SetBool ("isIdle", false);
                                 anim.SetBool ("isIdleLeft", false);
                                 anim.SetBool ("isIdleRight", false);
                             }
 
             else if (Input.GetKey(KeyCode.E) && Input.GetKey(KeyCode.W))
                             {
                                 anim.SetBool ("isWalking", false);
                                 anim.SetBool ("isRunning", true);
                                 anim.SetBool ("isIdle", false);
                                 anim.SetBool ("isIdleLeft", false);
                                 anim.SetBool ("isIdleRight", false);
                             }
 
             else if (Input.GetKey(KeyCode.Q) && Input.GetKey(KeyCode.S))
                             {
                                 anim.SetBool ("isWalking", false);
                                 anim.SetBool ("isRunning", true);
                                 anim.SetBool ("isIdle", false);
                                 anim.SetBool ("isIdleLeft", false);
                                 anim.SetBool ("isIdleRight", false);
                             }
 
             else if (Input.GetKey(KeyCode.E) && Input.GetKey(KeyCode.S))
                             {
                                 anim.SetBool ("isWalking", false);
                                 anim.SetBool ("isRunning", true);
                                 anim.SetBool ("isIdle", false);
                                 anim.SetBool ("isIdleLeft", false);
                                 anim.SetBool ("isIdleRight", false);
                             }
                     
                     }
 
                     //Walking
                     else if (Input.GetKey(KeyCode.W))
                         {
                         anim.SetBool ("isWalking", true);
                         anim.SetBool ("isRunning", false);
                         anim.SetBool ("isIdle", false);
                         anim.SetBool ("isIdleLeft", false);
                         anim.SetBool ("isIdleRight", false);
 
                         speed = walkSpeed;
                         }
 
                     else if (Input.GetKey(KeyCode.S))
                         {
                         anim.SetBool ("isWalking", true);
                         anim.SetBool ("isRunning", false);
                         anim.SetBool ("isIdle", false);
                         anim.SetBool ("isIdleLeft", false);
                         anim.SetBool ("isIdleRight", false);
 
                         speed = walkSpeed;
                         }
 
         else if (Input.GetKey(KeyCode.Q))
                     {
                         anim.SetBool ("isWalking", false);
                         anim.SetBool ("isRunning", false);
                         anim.SetBool ("isIdle", false);
                         anim.SetBool ("isIdleLeft", true);
                         anim.SetBool ("isIdleRight", false);
 
                         speed = walkSpeed;
                     }
 
         else if (Input.GetKey(KeyCode.E))
                     {
                         anim.SetBool ("isWalking", false);
                         anim.SetBool ("isRunning", false);
                         anim.SetBool ("isIdle", false);
                         anim.SetBool ("isIdleLeft", false);
                         anim.SetBool ("isIdleRight", true);
 
                         speed = walkSpeed;
                     }
 
         else if (Input.GetKey(KeyCode.Q) && Input.GetKey(KeyCode.W))
                         {
                         anim.SetBool ("isWalking", false);
                         anim.SetBool ("isRunning", false);
                         anim.SetBool ("isIdle", false);
                         anim.SetBool ("isIdleLeft", true);
                         anim.SetBool ("isIdleRight", false);
 
                         speed = walkSpeed;
                         }
 
         else if (Input.GetKey(KeyCode.E) && Input.GetKey(KeyCode.W))
                         {
                         anim.SetBool ("isWalking", false);
                         anim.SetBool ("isRunning", false);
                         anim.SetBool ("isIdle", false);
                         anim.SetBool ("isIdleLeft", false);
                         anim.SetBool ("isIdleRight", true);
 
                         speed = walkSpeed;
                         }
 
         else if (Input.GetKey(KeyCode.E) && Input.GetKey(KeyCode.S))
                     {
                         anim.SetBool ("isWalking", false);
                         anim.SetBool ("isRunning", false);
                         anim.SetBool ("isIdle", false);
                         anim.SetBool ("isIdleLeft", false);
                         anim.SetBool ("isIdleRight", true);
 
                         speed = walkSpeed;
                     }
 
         else if (Input.GetKey(KeyCode.E) && Input.GetKey(KeyCode.S))
                     {
                         anim.SetBool ("isWalking", false);
                         anim.SetBool ("isRunning", false);
                         anim.SetBool ("isIdle", false);
                         anim.SetBool ("isIdleLeft", false);
                         anim.SetBool ("isIdleRight", true);
 
                         speed = walkSpeed;
                     }
                 else 
                     {
                         //Is Idle
                         anim.SetBool ("isWalking", false);
                         anim.SetBool ("isRunning", false);
                         anim.SetBool ("isIdle", true);
                         anim.SetBool ("isIdleLeft", false);
                         anim.SetBool ("isIdleRight", false);
                     }
 
         //var translation = Input.GetAxis ("Vertical") * speed;
                 //var rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
 
                // transform.Translate (0, 0, translation);
                // transform.Rotate (0, rotation, 0);
 
                 if (Input.GetButtonDown ("Jump")) 
                     {
                         anim.SetTrigger ("isJumping");
                     }
     
             }
 }
  
 

And here is the mouse and camera control script attached to the player as well:

 using UnityEngine;
 using System.Collections;
 
 public class MouseAimCamera : MonoBehaviour {
     public GameObject target;
     public float rotateSpeed = 5;
     Vector3 offset;
     public float turnSpeed;
 
 
     void Start() {
         offset = target.transform.position - transform.position;
     }
 
 
 
     void LateUpdate() {
         if (Input.GetKey (KeyCode.W)) {
             
             float horizontal = Input.GetAxis ("Mouse X") * rotateSpeed;
             target.transform.Rotate (0, horizontal, 0);
 
             float desiredAngle = target.transform.eulerAngles.y;
             Quaternion rotation = Quaternion.Euler (0, desiredAngle, 0);
             transform.position = target.transform.position - (rotation * offset);
 
             transform.LookAt (target.transform);
         }
 
         if (Input.GetKey (KeyCode.S)) {
 
             float horizontal = Input.GetAxis ("Mouse X") * rotateSpeed;
             target.transform.Rotate (0, horizontal, 0);
 
             float desiredAngle = target.transform.eulerAngles.y;
             Quaternion rotation = Quaternion.Euler (0, desiredAngle, 0);
             transform.position = target.transform.position - (rotation * offset);
 
             transform.LookAt (target.transform);
         }
 
     }
 }

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 Hatsuko · Oct 27, 2017 at 11:57 PM 0
Share

Since your current camera control script is called "$$anonymous$$ouseAimCamera", I was wondering if you prefer to put the code about "rotate around player when idle" in the same script or not? For now I think maybe it's a good idea to create another script that does this job. For me it makes things simpler :) But it's up to you.

 

Translate and Rotate https://unity3d.com/learn/tutorials/topics/scripting/translate-and-rotate

 

Currently is your Hierarchy like this?

 Player
 â”” $$anonymous$$ain Camera

If you change it to

 Player
 â”” Camera Holder
     â”” $$anonymous$$ain Camera

Set "Camera Holder"s local position 0, 0, 0. By rotating "Camera Holder", you can make "$$anonymous$$ain Camera" rotate around "Player".

 

What's left is to come up a way to enable this camera rotation script when idle, and disable it when not idle.

 

Enabling and Disabling Components https://unity3d.com/learn/tutorials/topics/scripting/enabling-and-disabling-components

0 Replies

· Add your reply
  • Sort: 

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

120 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

Related Questions

How to clamp horizontal rotation of the camera? 1 Answer

MouseLook conflicting with LookAt 2 Answers

How to change cameras when changing scenes? 1 Answer

Trying to Make a Smooth Camera Zoom While Changing Targets 1 Answer

Cinemachine preserve rotation and position on transition 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