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 MacroNerd · Mar 18, 2016 at 11:56 PM · c#camerarotation

Camera Rotates when player rotates

Hi. I was using this code to rotate the camera when the player rotates:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
 
     public GameObject player;
 
     private Vector3 offset;
 
     // Use this for initialization
     void Start () {
         offset = transform.position - player.transform.position;
     }
 
     // Update is called once per frame
     void LateUpdate () {
         transform.position = player.transform.position + offset;
         transform.rotation = player.transform.position;
         transform.Rotate (new Vector3 (45, 00));
     }
 }
 

but the player wasn't the axis of rotation. So someone gave me this:

 public class CameraController : MonoBehaviour
  {
      public GameObject player;
      
      private Vector3 offset;
      
  
      void Start ()
      {
          offset = transform.position - player.transform.position;
      }
      
  
      void LateUpdate ()
      {
          //transform.position = player.transform.position + offset;
          transform.RotateAround (player.transform.position, Vector3.up, 100 * Time.deltaTime );
          //transform.Rotate (new Vector3 (45, 0, 0));
      }
  }

yet all this does is add a Star Wars like flyby of the player. How can i get the camera to rotate with the player, yet keeping them as the axis of rotation?

@Tymewiz thanks for the code and hope you can help with this one.

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 Tymewiz · Mar 20, 2016 at 11:43 AM

while i am not sure what you want, if you want something that is based on GTA-V while driving first create an empty gameobject and make it and the camera a child of the player and the empty gameobject is behind the player then add this script to the camera:

   using UnityEngine;
     using System.Collections;
     
 public class CameraController : MonoBehaviour
 {
     float verticalinput;
     float horizontalinput;
     [Range(0,1)]
     public float sensitivity = 0.001f;
     bool ijklpressed;
     float snappingdelay;
     public Transform Player;
     public Transform EmptyGameobject;
     
     //setting up input, could skip this step by setting the equivalent up in the input manager and setting horizontalinput
     // and vertinput equal to the axisname that was set up.
     //i'm doing it this way because you might already be using input.getaxis("Horizontal/vertical");
     // this setup uses IJKL
     
     
     void Update ()
     {
         if (Input.GetKey (KeyCode.J)) {
             
             horizontalinput -= sensitivity;
             ijklpressed = true;
         }
         if (Input.GetKey (KeyCode.L)) {
             
             horizontalinput += sensitivity;
             ijklpressed = true;
         }
         if (Input.GetKey (KeyCode.I)) {
             
             verticalinput += sensitivity;
             ijklpressed = true;
         }
         if (Input.GetKey (KeyCode.K)) {
             
             verticalinput -= sensitivity;
             ijklpressed = true;
         }
         if (Mathf.Abs (verticalinput) < Mathf.Epsilon) {
             verticalinput = 0;
         }
         if (Mathf.Abs (horizontalinput) < Mathf.Epsilon) {
             horizontalinput = 0;
         }
         if (!ijklpressed) {
             snappingdelay += Time.deltaTime;
             if (snappingdelay >= .25f) {
                 horizontalinput /= 1.01f;
                 verticalinput /= 1.01f;
             }
         } else {
             ijklpressed = false;
             snappingdelay = 0;
         }
         
         
         horizontalinput = Mathf.Clamp (horizontalinput, -1, 1);
         verticalinput = Mathf.Clamp (verticalinput, -1, 1);
     }
     void LateUpdate ()
     {
         transform.LookAt (Player);
         transform.localPosition = EmptyGameobject.localPosition + new Vector3 (horizontalinput, verticalinput, 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

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

135 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

Related Questions

Rotation around player. and Camera position, 2 Answers

How can I stop my first person camera from going upside down? 0 Answers

Rotate towards mouse pointer 1 Answer

How to access y rotation of an object as a variable or value 1 Answer

Rotating camera not working? 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