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 kewlsnake · Sep 01, 2013 at 03:45 PM · transform.rotationfirst personmouse looktransform.lookat

getting out of LookAt messes up first person view

I'm having problems with getting out of transform.LookAt.

Imagine having a sphere that attracts a player and also locks the player's gaze at it until it is within a certain radius. When it is within that radius, the player can look normally again. The problem I have is that the view becomes slanted when going from right to left with the mouse as shown here.

This is the script I'm using.

 using UnityEngine;
 using System.Collections;
 
 public class MoveToObject : MonoBehaviour
 {
     public float speed = 1.0f;
     public GameObject player;
     // Use this for initialization
     void Start ()
     {
     }
     
     // Update is called once per frame
     void Update ()
     {
 Vector3 objectPosition = transform.position;
 Vector3 playerPosition = player.transform.position;
         if (Vector3.Distance (objectPosition, playerPosition) > 5.0f) {
             playerPosition += speed * (objectPosition - playerPosition).normalized * Time.deltaTime;
             player.transform.position = playerPosition;
             player.transform.LookAt (objectPosition);
         }
     }
 }
 

One thing I tried was saving the original player.transform.rotation in a variable before getting attracted by the sphere and then restoring it later. This made the player face the same direction as he did originally and that's not what I want.

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

1 Reply

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

Answer by DESTRUKTORR · Sep 01, 2013 at 03:55 PM

The whole player is rotated. Are you using a character controller? You should attach the camera to a child of the controller, and have the camera's transform look at the object. If you switch to the scene view after the mouse gets screwed up, you'll note that the collider for the player is pointed in the same direction as the camera, I'd wager.

Comment
Add comment · Show 1 · 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 kewlsnake · Sep 02, 2013 at 05:27 AM 0
Share

Thank you, that was the problem. Unfortunately I wasn't able to apply an elegant fix by letting the player camera look at the object. It seems to be somewhat disconnected from the player when inside the radius. I was using the standard First Person Controller by the way.

What I did do is do away with LookAt / LookRotation entirely and manually rotated the player. This was based on code found here

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$oveToObject : $$anonymous$$onoBehaviour
 {
     public float speed = 1.0f;
     public GameObject player;
         private GameObject target;
     private Vector3 targetPoint;
     private Quaternion targetRotation;
     // Use this for initialization
     void Start ()
     {
     }
     
     // Update is called once per frame
     void Update ()
     {
 Vector3 objectPosition = transform.position;
 Vector3 playerPosition = player.transform.position;
         if (Vector3.Distance (objectPosition, playerPosition) > 5.0f) {
         // movement player to object
             playerPosition += speed * (objectPosition - playerPosition).normalized * Time.deltaTime;
             player.transform.position = playerPosition;
             
             
             //replacement for LookAt / LookRotation, forces gaze of player onto object
             targetPoint = new Vector3 (objectPosition.x, playerPosition.y, objectPosition.z) - playerPosition;
             targetRotation = Quaternion.LookRotation (targetPoint, Vector3.up);
             player.transform.rotation = Quaternion.Slerp (player.transform.rotation, targetRotation, Time.deltaTime * 10f);
         }
     }
 }
 

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

17 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 avatar image avatar image avatar image avatar image

Related Questions

Vertical Camera Orbit 0 Answers

How do I add force to the direction a game object is facing? 2 Answers

Upper body look at problem 1 Answer

Question about positive and negative imput values. 2 Answers

transform.eulerAngles of camera - changing the speed of rotation not working? 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