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 caiodmk · Oct 03, 2011 at 08:56 AM · camerarotationplayerlookat

Player rotates with camera(face same direction as the camera)

I have a 3rd person game where the camera moves around the player, the player is the axis and when i move the mouse the camera rotates around him. What i want to do is have my player turning with the camera, so I'll always see the back of my character. What i tried to do is to add a EmptyGameObject(focoVisao) in the middle of the far clipping plane, and use transform.LookAt(focoVisao). It kinda works, but if I look up my character will rotate on the X axis and if I press W i can just move towards the sky. I want it to only rotate on the Y axis.

Here are the codes I'm using:

For the Player:

 using UnityEngine;
 
 class Move : MonoBehaviour // Sempre que houver a necessidade de mexer na cena tem de herdar a classe MonoBehavior
 {
     public int vel = 5; // Se não colocar nada o programa entende a variavel como privada
     public Transform spawPoint;
     public Transform focoVisao;
 
 
 
     public void Update()
     {
         transform.Translate(vel * Time.deltaTime * Input.GetAxis("Horizontal"), 0, vel * Time.deltaTime * Input.GetAxis("Vertical")); // Time.deltaTima representa o tempo de resposta entre os frames
         transform.LookAt(focoVisao);
     }
     //colides with the rocks(die)
     public void OnCollisionEnter(Collision hit)
     {
         if(hit.gameObject.tag == "rochas")
         {
             transform.position = spawPoint.position;
         }
 
     }
 
     
 }

And for the Camera:

 using UnityEngine; 
 using System.Collections; 
 
 public class CameraScript : MonoBehaviour 
 { 
     public Transform target; 
     
     public float targetHeight = 1.7f; 
     public float distance = 5.0f;
     public float offsetFromWall = 0.1f;
 
     public float maxDistance = 20; 
     public float minDistance = .6f; 
 
     public float xSpeed = 200.0f; 
     public float ySpeed = 200.0f; 
 
     public int yMinLimit = -80; 
     public int yMaxLimit = 80; 
 
     public int zoomRate = 40; 
 
     public float rotationDampening = 3.0f; 
     public float zoomDampening = 5.0f; 
     
     public LayerMask collisionLayers = -1;
 
     private float xDeg = 0.0f; 
     private float yDeg = 0.0f; 
     private float currentDistance; 
     private float desiredDistance; 
     private float correctedDistance; 
 
     void Start () 
     { 
         Vector3 angles = transform.eulerAngles; 
         xDeg = angles.x; 
         yDeg = angles.y; 
 
         currentDistance = distance; 
         desiredDistance = distance; 
         correctedDistance = distance; 
 
         // Make the rigid body not change rotation 
         if (rigidbody) 
             rigidbody.freezeRotation = true; 
     } 
     
     /** 
      * Camera logic on LateUpdate to only update after all character movement logic has been handled. 
      */ 
     void LateUpdate () 
     { 
         Vector3 vTargetOffset;
         
        // Don't do anything if target is not defined 
         if (!target) 
             return; 
 
         // Mouse Controla Camera 
             xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02f; 
             yDeg -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02f; 
 
         yDeg = ClampAngle (yDeg, yMinLimit, yMaxLimit); 
 
         // set camera rotation 
         Quaternion rotation = Quaternion.Euler (yDeg, xDeg, 0); 
 
         // calculate desired camera position
         vTargetOffset = new Vector3 (0, -targetHeight, 0);
         Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset); 
 
         RaycastHit collisionHit; 
         Vector3 trueTargetPosition = new Vector3 (target.position.x, target.position.y + targetHeight, target.position.z); 
 
         
         bool isCorrected = false; 
 
         if (Physics.Linecast (trueTargetPosition, position, out collisionHit, collisionLayers.value)) 
         { 
             correctedDistance = Vector3.Distance (trueTargetPosition, collisionHit.point) - offsetFromWall; 
             isCorrected = true;
         }
 
         currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance; 
 
         currentDistance = Mathf.Clamp (currentDistance, minDistance, maxDistance); 
 
         position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset); 
         
         transform.rotation = rotation; 
         transform.position = position; 
 
     } 
 
     private static float ClampAngle (float angle, float min, float max) 
     { 
         if (angle < -360) 
             angle += 360; 
         if (angle > 360) 
             angle -= 360; 
         return Mathf.Clamp (angle, min, max); 
     } 
 } 
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 SilverTabby · Oct 03, 2011 at 10:48 AM 1
Share

I had a similar problem with the thing I was trying to work with.

What I ended up doing was:

  • parent the camera to the player

  • make the left-right axis turn only the player left-right

  • Because the camera is parented to the player, the camera will always follow him perfectly

  • make the up-down axis tilt only the camera

It seems to work for me. As far as I can tell, it's the exact same system they used with the 3rd person mode in Fallout 3/Vegas.

Just keep in $$anonymous$$d that you generally want to move the player in relation to the Camera's transform, not to the player model's transform

1 Reply

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

Answer by caiodmk · Oct 20, 2011 at 12:15 AM

  • make the left-right axis turn only the player left-right.

  • make the up-down axis tilt only the camera

That solved it, thanks @SilverTabby

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Trying to have object spin within player and always face "up" relative to camera [example pics of what I'm trying to achieve in post]] 0 Answers

Rotating objects to specific angle 3 Answers

Changing The Camera Position & Offsetting LookAt 2 Answers

Get a child object to rotate a parent to the cameras direction -1 Answers

how to make player looks at mouse 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