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 · May 11, 2013 at 01:35 AM · cameracharacteranglefacing

Camera not facing back of character

Ok so my problem is as such. I have a character model in my game. The character model is the parent to an empty game object named CameraTarget which is being used as, you guessed it, the target for my camera.I have the script below attached to the main camera with the CameraTarget set as it's target.

The problem is that no matter what direction my character is facing, the CameraTarget is facing, the camera itself is facing when I click play rather than defaulting to the back of my characters head like I want it to, it always points off in the same direction. If I hold down the right mouse button down and look around and let go it snaps back to the same spot (as it should) but I find that this often times means it ends up snapping to the side of his face, or even the front of his face (depending on which way he is facing at the time.

How can I get the camera to realize it should be facing the BACK of the target?

 using UnityEngine;
 using System.Collections;
 
 public class Camera : MonoBehaviour 
 {
     
     public Transform target;
     public string playerTagName = "Player";
     public float walkDistance;
     public float runDistance;
     public float sprintDistance;
     public float cameraHeight;
     public float heightDampening = 2.0f;
     public float rotationDampening = 3.0f;
     private float _x;
     private float _y;
     
     public float xSpeed = 250f;
     public float ySpeed = 120f;
     
     private bool cameraButtonDown;
     
     private Transform _myTransform;
     
     void Awake()
     {
         _myTransform = transform;        
     }
     // Use this for initialization
     void Start () 
     {
         if(target == null)
             Debug.Log("The camera does not have a target.");
         else
         {
         CameraSetup();
         }
     }
     
     void Update()
     {
         if(Input.GetMouseButtonDown(1))        //Use the input manager to make this user selectable
         {
             cameraButtonDown = true;
         }

         if(Input.GetMouseButtonUp(1))        //Use the input manager to make this user selectable
         {
             cameraButtonDown = false;
         }
     }
     
     void LateUpdate()
     {
         if(target != null)
         {
             if(cameraButtonDown)        //Use the input manager to make this user selectable
             {
             Debug.Log("Right Mouse");
             _x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
             _y -= Input.GetAxis("Mouse Y") * xSpeed * 0.02f;
             
             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 + cameraHeight, target.position.z - walkDistance);
             _myTransform.LookAt(target);
             
             _x = 0;
             _y = 0;
                 
             float l_wantedRotationAngle = target.eulerAngles.y;
             float l_wantedHeight =  target.position.y + cameraHeight;
                 
             float l_currentRotationAngle = _myTransform.eulerAngles.y;
             float l_currentHeight = _myTransform.position.y;
                 
             l_currentRotationAngle = Mathf.LerpAngle(l_currentRotationAngle, l_wantedRotationAngle, rotationDampening * Time.deltaTime);
                 
             l_currentHeight = Mathf.Lerp(l_currentHeight, l_wantedHeight, heightDampening * Time.deltaTime);
                 
             Quaternion currentRotation = Quaternion.Euler(0, l_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, l_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 + cameraHeight, 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 Ajaxx84 · May 11, 2013 at 12:34 PM 0
Share

I have tried using a different character model to see if it was something with the model, even though it didn't seem model related, and got the same result. Still can't figure out why this thing always wants to point the same direction. Still looking for help.

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

13 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

Related Questions

Rotate camera to character angle 0 Answers

Move character in direction its facing 1 Answer

How to limit the angle of the camera? 1 Answer

How To Set Camera Angle With A Variable? 0 Answers

Camera Follow Positive Y only - C# 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