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
2
Question by blisher · Feb 11, 2014 at 09:22 PM · c#third-persontps

Transform.position returns local position

Hello there, I am IT student and I'm messing around Unity, making TPS style game. From time to time, my CameraMovementScript seems to crash and transform.position returning local position of following pivot, as a result - camera is following global (0,0,0) point instead of player.

To fix this I only need to restart Unity. Then it happens again in few times "Play" clicked.

Comment
Add comment · Show 5
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 robertbu · Feb 11, 2014 at 10:18 PM 1
Share

It is highly unlikely to be a Unity problem and very likely to be an issue with your script. We need to see your script for any kind of specific answer.

avatar image blisher · Feb 11, 2014 at 10:22 PM 0
Share

@robertbu Cannot be my script issue, once I click "play" camera follows global 0,0,0. Then i restart unity without ANY changes, click play again and camera follows player

avatar image rutter · Feb 11, 2014 at 11:23 PM 0
Share

That doesn't conclusively rule out a script issue; as an example, you could have a race condition during the first frame. Are you seeing any error messages in the console window?

avatar image Owen-Reynolds · Feb 11, 2014 at 11:57 PM 0
Share

Doesn't sound like an issue where position == localPosition.

LocalPosition is only a thing if you are exa$$anonymous$$ing a child object. It's the position relative to the parent. If an object has no parent, localPos is the same as (global) position.

Is your "follow pivot" an empty child behind the player? Even so, try printing out the pivot.position to check. If it suddenly starts printing (0,2,-10) over and over, no matter how the player spins and moves, then yes, position is glitching and returning localPosition.

avatar image blisher · Feb 12, 2014 at 10:58 AM 0
Share

I don't clearly understand the issue, but another thing I observed is that while running, dirt smoke should be appear at (0,0,0) local foot position which changes during run. When problem occurs, all footstep smokes are visible at global (0,0,0)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Slayer1928 · Feb 11, 2014 at 10:33 PM

Try another camera follow script?

 public Transform target;
     public float distance = 10.0f;
     public float height = 3.0f;
     public float heightDamping = 2.0f;
     public float rotationDamping = 3.0f;
  
     void LateUpdate () {
        if (!target)
          return;
  
        Debug.Log(distance.ToString());
        float wantedRotationAngle = target.eulerAngles.y;
       float wantedHeight = target.position.y + height;
  
        float currentRotationAngle = transform.eulerAngles.y;
       float currentHeight = transform.position.y;
  
        currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
  
        currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
  
        Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
  
        transform.position = target.position;
        transform.position -= currentRotation * Vector3.forward * distance;
        Vector3 temp = transform.position;
        temp.y = currentHeight; 
        transform.position = temp;
  
        transform.LookAt (target);
     }
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 blisher · Feb 12, 2014 at 10:54 AM 0
Share

I am using exatly the same script as yours, with different variable names

avatar image
0

Answer by qwinzeth · May 15, 2019 at 03:51 AM

I basically used GameObject.Find("Parent").transform.Find("Child").position to work around what I believe to be a similar issue.


My similar, strange problem is that a prefab ("Boss") contains a child ("CameraAnchor") and GameObject.Find("CameraAnchor") would sometimes, every now and then, return (0,4.5,0) as transform.position, which is actually the localPosition. It seemed to happen sometimes when restarting the scene, but not reliably. Usually transform.position would return the expected global position of (230, 4.5, 0)


I was able to work around this strange problem by grabbing GameObject.Find("Boss"), and then using transform.Find("CameraAnchor")


Here is my mess of a function I eventually cobbled together. Most code probably can't use "x > 1" as a "valid" position, but it works for me because a Boss is never at the beginning of a level :)

  public void LockCamera(Vector2 lockPosition)
     {
         // This is normal, everything is fine
         if (lockPosition.x >= 1)
         {
             _isPositionLocked = true;
             _lockPosition = lockPosition;
             return;
         }
 
         // Uh oh, Unity done goofed and gave us the localPosition for some reason...
         Debug.Log("INVALID CameraScript.LockCamera(lockPosition = " + lockPosition);
         GameObject bossObject = GameObject.Find("Boss");
         // Serious problem here
         if (bossObject == null)
         {
             Debug.Log("Unable to Find Boss object, aborting LockCamera");
             return;
         }
         Debug.Log("Boss at " + bossObject.transform.position);
         _lockPosition = bossObject.transform.position;
         if (_lockPosition.x >= 1)
         {
             _isPositionLocked = true;
         }
 
         // We can try to get the CameraAnchor ourselves
         Transform cameraAnchorObjectTransform = bossObject.transform.Find("CameraAnchor");
         if (cameraAnchorObjectTransform == null)
         {
             // Well, we tried.
             Debug.Log("Unable to Find CameraAnchor, using Boss if able");
             return;
         }
         else if (cameraAnchorObjectTransform.position.x >= 1)
         {
             Debug.Log("Found CameraAnchor at " + cameraAnchorObjectTransform.position);
             _lockPosition = cameraAnchorObjectTransform.position;
             _isPositionLocked = true;
             return;
         }
 
         Debug.Log("Found CameraAnchor at INVALID, using Boss if able " + cameraAnchorObjectTransform.position);
     }




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

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Mouse orbit and problems with raycast 0 Answers

Renderer on object disabled after level reload 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in 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