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 RiddarN · Apr 30, 2015 at 06:26 PM · c#camerarotationtopdownsimple

I need some help with camera rotations

Well, I am trying to make an effect on my 3d top-down shooter game. This effect includes the camera's rotation. Basically, what I want to do, is to keep the camera at the player, but when I move the mouse on the screen (lets just say I move it top right), the camera should extend towards that point, but never actually look away from the player.

This works, as long as I only use either the X or Y axis. If I use them both, the entire camera freaks out and starts swinging wide everywhere.

My code this far (It's on the camera):

 using UnityEngine;
 using System.Collections;
 
 public class PlayerCamera : MonoBehaviour {
 
     public Vector3 offset = new Vector3(0,0,0);
     public float cameraReach = 16;
 
     float xRotation = 0.0f;
     float yRotation = 0.0f;
 
     GameObject player;
 
     void Start () 
     {
         player = GameObject.Find("Plr_Player");
     }
 
     void Update () 
     {
         //xRotation = ((Input.mousePosition.x - (Screen.height / 2)) / cameraReach);
         //yRotation = ((Input.mousePosition.y - (Screen.height / 2)) / cameraReach);
 
         xRotation = (-transform.rotation.eulerAngles.x + (((Input.mousePosition.x - (Screen.height / 2)) / cameraReach)));
         yRotation = (-transform.rotation.eulerAngles.y  - (((Input.mousePosition.y - (Screen.height / 2)) / cameraReach)));
     }
 
     void LateUpdate () 
     {
         transform.position = player.transform.position + offset;
         transform.rotation = Quaternion.Euler(yRotation, xRotation , transform.rotation.eulerAngles.z);
     }
 }
 

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 maccabbe · Apr 30, 2015 at 06:27 PM 0
Share

Try replacing

 xRotation = (-transform.rotation.eulerAngles.x + (((Input.mousePosition.x - (Screen.height / 2)) / cameraReach)));
 yRotation = (-transform.rotation.eulerAngles.y  - (((Input.mousePosition.y - (Screen.height / 2)) / cameraReach)));

with

 xRotation = (-xRotation  + (((Input.mousePosition.x - (Screen.height / 2)) / cameraReach)));
 yRotation = (-yRotation  - (((Input.mousePosition.y - (Screen.height / 2)) / cameraReach)));

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

Flip over an object (smooth transition) 3 Answers

Camera to follow the player in the form of radius 1 Answer

How to rotate camera around object smoothly in fixed intervals 1 Answer

Can't do Quaternion.AngleAxis twice. 1 Answer

Limit Rotation around X Axis? 3 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