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 zero_null · Jan 03, 2018 at 12:49 PM · camera-movementcamera follow

Why Child's Local z position is changing when I change z of my camera.

I have a scene where I have my background sprite positioned at 10 local unit z under camera. I have a perspective camera. My background is child of my camera. I am changing z of my camera but it's automatically changing z of background. Like when I change my z to -20 camera child background object's z also change to 20 How can I avoid it ?

My Camera Movement / Following script

 using UnityEngine;
 public class CameraController : MonoBehaviour
 {
     Vector3 newPos;
     public Vector3 camOffSet = Vector2.zero;
     public float speed = 2;
     Vector3 TargetPos
     {
         get
         {
             return CarController.Instance.transform.position;
         }
     }
 
     void FixedUpdate()
     {
         if (CarController.Instance != null)
         {
             newPos = TargetPos + camOffSet;
             //if (!CarController.Instance.IsGrounded && CarController.Instance.CanZoomOut)
             //{
             //    float zz = Mathf.Clamp(-10 - newPos.y, -20, -10);
             //    newPos.z = Mathf.Lerp(transform.position.z, Mathf.Clamp(-10 - newPos.y, -20, -10), speed * Time.deltaTime);
             //    //newPos.z = zz;
             //    newPos.x -= zz * 0.2f;
             //}
             //else
             {
                 newPos.z = Mathf.Lerp(transform.position.z, -10, speed * Time.deltaTime);
             }
 
             transform.position = newPos;// Vector3.MoveTowards(transform.position, newPos, speed * Time.deltaTime);
         }
     }
 }
 

My Bg Move/follow script

 public float speed;
     Vector3 targetPos = Vector2.zero;
     
     void Update()
     {
         if (CarController.Instance != null)
         {
             transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
         }
     }


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 ScaniX · Jan 03, 2018 at 02:16 PM 0
Share

How are you checking the child position? transform.position of the child will change of course, because it is the global position while transform.localPosition should still be (0, 0, -10). Also you should have no script on the background at all that is changing the position.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Jan 03, 2018 at 01:08 PM

Hello!

If you change the transform.position of a gameobject, all gameobjects childs will move with it. If the child is not moving where it should, can be for these 2 reasons:

  • There is another script changing its position

  • The parent GameObject (the camera) have some scale/rotation value that is making its children move "wrong"

Try it, post some Images of the camera inspector

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 zero_null · Jan 03, 2018 at 01:26 PM 0
Share

No, No script is changing position of child. ALso rotation and scale of camera is also not being moved. I am have updated question to attach my code that moves my camera and my bg.

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

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

Related Questions

camera script not working 1 Answer

Smooth camera script looking at left hand side of character. 0 Answers

How to make a camera Follow an Object moving in zigzag path? 1 Answer

Camera Follow (Not Exact) 1 Answer

Camera Follow works partially, need help to finish the script 3 Answers


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