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 /
avatar image
0
Question by MySecretWeapon · Aug 26, 2011 at 10:58 PM · c#camerarpg

Camera look position is always fixed.. need help.

I have a camera script that i've written for my RPG. The camera allows the player to click the right mouse button and look around. This feature works great. When you let go of the button the camera gently floats behind the player again. The problem is, when you click the right mouse button again, the camera always starts at a fixed rotation (0,0,0) no matter which way the character is turned. I need that rotation to always be looking at the player... Any help I can get would be great.

here's the script C#:

 using UnityEngine;
 using System.Collections;
 
 public class RPGCamera : MonoBehaviour {
     
     public Transform target;
     public string playerTagName = "Player";
     
     public float walkDistance;
     public float runDistance;
     public float height;
     public float xSpeed = 250.0f;
     public float ySpeed = 120.0f;
     public float heightDamping = 2.0f;
     public float rotationDamping = 2.0f;
     
     private Transform _myTransform;
     private float _x;
     private float _y;
     private bool _camButtonDown = false;
     
     
     
     void Awake(){
         _myTransform = transform;
     }
     // Use this for initialization
     void Start () {
         if(target == null)
             Debug.LogWarning("We do not have a target");
         else{
             CameraSetup();
     
         }
         
         
         
     }
     void Update (){
         if(Input.GetMouseButtonDown(1)){    //Use the Input Manager to make this user selectable.
             _camButtonDown = true;
         }
         if(Input.GetMouseButtonUp(1)){
             _camButtonDown = false;    
         }
         if(Input.GetAxis("Mouse ScrollWheel") < 0){
             walkDistance += 1;    
         }
         if(Input.GetAxis("Mouse ScrollWheel") > 0){
             walkDistance -= 1;    
         }
         if(walkDistance > 10){
             walkDistance = 10;    
         }
         if(walkDistance < 5)
             walkDistance = 5;
         }
     
     void LateUpdate(){
         if(target != null){
             if(_camButtonDown){    
             _x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                _y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
          
 //             _y = ClampAngle(_y, yMinLimit, yMaxLimit);
                 
                 Quaternion rotation = Quaternion.Euler(_y, _x, 0);
                 Vector3 position = rotation * new Vector3(0.0f, 0.0f, -walkDistance) + target.position;
         
                 _myTransform.rotation = rotation;
                 _myTransform.position = position;    
             }
         else{
 //            _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
 //            _myTransform.LookAt(target);
             _x = 0;
             _y = 0;
                 
             // Calculate the current rotation angles
                 float wantedRotationAngle = target.eulerAngles.y;
                 float wantedHeight = target.position.y + height;
                     
                 float currentRotationAngle = _myTransform.eulerAngles.y;
                 float currentHeight = _myTransform.position.y;
                 
                 // Damp the rotation around the y-axis
                 currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
             
                 // Damp the height
                 currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
             
                 // Convert the angle into a rotation
                 Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
                 
                 // Set the position of the camera on the x-z plane to:
                 // distance meters behind the target
                 _myTransform.position = target.position;

             _myTransform.position -= currentRotation * Vector3.forward * walkDistance;
         
             // Set the height of the camera
             _myTransform.position = new Vector3(_myTransform.position.x, currentHeight, _myTransform.position.z);
             
             // Always look at the target
             _myTransform.LookAt (target);
         
     }
 }
     else{
         GameObject go = GameObject.FindGameObjectWithTag(playerTagName);
         
         if(go == null)
             return;
         target = go.transform;
     }
     
     
             
 }
 public void CameraSetup(){
         _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
         _myTransform.LookAt(target);
 }

}

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 MySecretWeapon · Aug 27, 2011 at 01:58 AM 1
Share

Yeah, I'm sorry about that, it's one of a few that I'm still working on cleaning up. I need to get in the habit of organizing as I do it.

3 Replies

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

Answer by Waz · Aug 26, 2011 at 11:58 PM

You need to set _x and _y to current camera Euler angles when mouse is clicked.

Comment
Add comment · Show 2 · 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 MySecretWeapon · Aug 27, 2011 at 01:57 AM 1
Share

worked great, just tossed in:

_x = transform.eulerAngles.y;

_y = transform.eulerAngles.x;

and it works great!

You're my new best friend! lol

Thank you!

avatar image Waz · Aug 27, 2011 at 11:55 AM 0
Share

It's slightly more efficient to get eulerAngles in one call:

 var e = transform.eulerAngles;
 _x = e.y;
 _y = e.x;

Not a big deal though.

avatar image
-2

Answer by cvgwegwegwe · Aug 27, 2011 at 12:35 PM

Removed for spamming.

Please ban this guy.

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

Answer by cvgwegwegwe · Aug 27, 2011 at 12:35 PM

Removed for spamming.

Please ban this guy.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Code That Is Scene Dependent 1 Answer

camera script, allow free movement of the camera up to a certain distance. 1 Answer

Toggle Control between multiple turrets 0 Answers

A fast triangle triangle intersection algorithm for unity? 4 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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