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 Ajaxx84 · Jan 08, 2013 at 05:42 AM · c#camerarotate

Why is my camera switching angles?

Ok so I have been following along with the Hack and Slash tutorial series at BurgZergArcade.com

I have gone through the step of creating a camera script to follow our player and allow for right click features.

Either I(we) messed something up in the code or it simply isn't what I am wanting from the script.

when I right click the camera always switches angles to face the same direction no matter what direction my character is facing. I want it to always lock on a behind view placing the back of my character on the screen when I right click as well as be able to turn the character by moving the mouse on the x axis. The latter might have to be done in a diff script, not sure. anyways here is the code. Can anyone explain to me why the camera is locking on to that specific direction when I right click instead of locking on to the back of my character?

using UnityEngine; using System.Collections;

public class HackAndSlashCamera : MonoBehaviour { public Transform target; public string playerTagName = "Player";

 public float minDistance;
 public float maxDistance;
 public float height;
 public float xSpeed = 250.0f;
 public float ySpeed = 120.0f;
 public float heightDamping = 2.0f;
 public float rotationDamping = 3.0f;
 
 
 private Transform _myTransform;
 
 private float _x;
 private float _y;

 private bool _cameraButtonDown = false;
 
 
 void Awake()
 {
     _myTransform = transform;
 }
 
 // Use this for initialization
 void Start () 
 {
     if(target == null)
         Debug.LogWarning("We do not have a target");
     else
     {
         CameraSetup();
     }

 }
 
 // Character moves in the update
 void Update () 
 {
     if(Input.GetMouseButtonDown(1))
     {
         _cameraButtonDown = true;
     }
     if(Input.GetMouseButtonUp(1))
     {
         _cameraButtonDown = (false);
     }
 }
 
 // Camera moves in the late update
 void LateUpdate()
 {    
     if(target != null)
     {
         if(_cameraButtonDown)            //Use the Input Manager to make this a user selectable button.
     {
         //Debug.Log("Right Mouse");
         
         _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, -minDistance) + target.position;
         
         _myTransform.rotation = rotation;
         _myTransform.position = position;

     }
     else
     {
     //controls the camera location
     //_myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - minDistance);
     //Makes camera look at the target.
     //_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 * minDistance;
 
     // 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()
 {
     //controls the camera location
     _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - minDistance);
     //Makes camera look at the target.
     _myTransform.LookAt(target);
 }

}

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Toggle Control between multiple turrets 0 Answers

This is a sloppy conversion from javascript to C#, I cannot seem to get it working... 2 Answers

problem wtih the script attachment 0 Answers

How do I rotate a Rigidbody to a surface normal AND use camera rotation? 1 Answer


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