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
1
Question by verer · Jun 15, 2015 at 04:40 AM · c#cameravector3variablelocalposition

How can i change a childs local position with a variable?

Hey, I'm just getting started on unity and I'm trying to make my own custom camera to learn the ropes rather then copying an already made camera, however I've ran into a problem. I'm incrementally making the script so at the moment I'm working on zoom and I've got the following set up for my camera:

 CameraController (Gameobject)
  |-CameraFocus (Gameobject)
    |-MainCamera (camera)

The camera is intended to be like an assassins creed camera except you can zoom in and out.

The CameraController simply follows the players position.

The CameraFocus is to control the rotation and act as the "focal point" of the camera.

And the camera itself is just meant to sit inside the camera focus and only have it's z position changed which would be the distance (zoom) from the players character.

Here's the script i have so far which is attached to the CameraController:

 public Transform playerTransform;
 private Camera cam;
 public Vector3 camDistance;
 public float zoom = 5.0f;

 void Start () {
     playerTransform = GameObject.FindGameObjectWithTag ("Player").transform;
     cam = Camera.main;
 }

 void Update (){
     camDistance = new Vector3(0,0,-zoom);
     if (cam) {
         cam.transform.localPosition = camDistance;
     }
 }

 void LateUpdate () {
     if (playerTransform) {
         this.transform.position = new Vector3 (playerTransform.position.x, playerTransform.position.y+1, playerTransform.position.z);
     }
 }

However this results in the camera sticking to the center of the CameraFocus for some reason (0,0,0 local position) but if i don't use the zoom variable and instead use an actual negative number like -10 for example it will work. However i would like to have a variable that i can change and have it zoom on the fly rather than be fixed.

I've searched and found nothing in relation to this specific case in using variables when changing a childs local position. It seems odd that it will work with actual number but not with a variable holding a number.

So how would i go about using a variable to control the local z position of my camera, would i need to take another approach?

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 duck · Jun 15, 2015 at 09:06 AM

Your concept is sound. You should be able to achieve what you want using the ideas in your question, you don't need another approach.

There must be some small problem with your code, or setup, that is causing the camera not to move. I suggest you double check what is happening at each line through your code by using Debug.Log to make sure your variables contain what you think they do.

For example, if the camera in your scene isn't tagged Main Camera, then line 8 above will put the value void into cam. Then because you have if (cam) { on line 13, it will silently fail giving no errors. Perhaps this is your problem. You can discover this by either removing the if statement, or tracing out the value of cam using Debug.Log.

Alternatively, there is a good general purpose following camera prefab in the Standard Assets, which operates in a similar way but has more features (including wall avoidance).

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 verer · Jun 15, 2015 at 10:22 AM 0
Share

Thank you so much! i debug logged the cam and found that because the cam was declared as a camera it wouldn't work the same way as a gameobject would when changing the local position for some reason. So all i did was just find the camera gameobject by tag and assign its gameobject to cam which fixed it :)

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

Vector3 is a 'type' but it is used like an 'variable' (C#) 1 Answer

Camera Lerp from A to B, and back 2 Answers

Adding a local position with InverseTransformPoint works in z axis, but inverts in x axis 0 Answers

Convert type UnityEngine.Vector3' to float' Error 1 Answer

RTS Camera movement wrong after rotation 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