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 Aezorion · Apr 07, 2015 at 12:38 AM · c#cameramovementrotate

I'm trying to write a script that will follow my player AND rotate around the character when the arrow keys are pressed. I'm close, but I'm missing something or have too much. Help? (Code inside)

My game is in a 3D world, and with characters and interactable objects being in 2D. (At least, that is my eventual goal) (similar to a game on Steam called Delver). Here's a link to some example gameplay: https://www.youtube.com/watch?v=KSetHTZj6rE

As the title states, I want my camera to both follow my character AND rotate upon request via the arrow keys. The best example I have to my desired camera wants is how Runescape works with it's camera following your character. I've searched far and wide and I'm just having no luck what-so-ever.

My camera does follow my character, but while following (and not pressing the arrow keys) my camera rotates (appears to be vertical) by itself. I want the camera to stay stationary, in a sense. Another issue I'm experiencing is that, while my horizontal camera rotation seems fine (as it rotates along the player's y-axis), any vertical rotations rotate along the player's x axis (in theory should be fine) but it's along the player's current world x-axis, and not the player's own x-axis. Any help would be AMAZING.

Finally, here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
 
     public Transform target;
     public float angularSpeed;
     
     [SerializeField][HideInInspector] 
     private Vector3 initialOffset;
     
     private Vector3 currentOffset;
     
     [ContextMenu("Set Current Offset")]
     private void SetCurrentOffset () {
         if(target == null) {
             return;
         }
         
         initialOffset = transform.position - target.position;
     }
     
     private void Start () {
         if(target == null) {
             Debug.LogError ("Assign a target for the camera in Unity's inspector");
         }
         
         currentOffset = initialOffset;
     }
     
     private void LateUpdate () {
         transform.position = target.position + currentOffset;
         
         float movementHorizontal = Input.GetAxis ("Horizontal") * angularSpeed * Time.deltaTime;
         float movementVertical = Input.GetAxis ("Vertical") * angularSpeed * Time.deltaTime;
         if(!Mathf.Approximately (movementHorizontal, 0f)) {
             transform.RotateAround (target.position, Vector3.up, movementHorizontal);
             currentOffset = transform.position - target.position;
         }
         if (!Mathf.Approximately (movementVertical, 0f) && movementHorizontal == 0) {
             transform.RotateAround (target.position, Vector3.right, movementVertical);
             currentOffset = transform.position - target.position;
         }
     }
 }
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

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

2 People are following this question.

avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Regarding transform.position in the roll a ball tutorial 1 Answer

Standard asset question 1 Answer

Arduino with a sensor for Homebrew VR headset 0 Answers

Disable Camera Movement,Stop camera movement 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