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 sadek9 · Nov 05, 2014 at 11:55 AM · camerapositionchangesliderhierarchy

Change position - cameras - slider

Hello everyone!

I have some probelm, because I am begginer in scripting.

I have attached two cameras to one GameObject, both are childs for GameObjcet in hierarchy. I want to add script to GameObject, which allows me to change positions each of cameras simultaneously thanks to one slider.

For example: I move slider, one of cameras move ten units to the right and simultaneously second camera move the same number of units to the left.

Can anyone show me script, which let me do this thing?

My script for now:


using UnityEngine; using System.Collections;

public class hSlider : MonoBehaviour {

 private float hSliderValue = 0.0f;
 
 void OnGUI () {
     hSliderValue = GUI.HorizontalSlider (new Rect (25, 25, 100, 30), hSliderValue, 0.0f, 10.0f);
 }

 void Update() {
     transform.Translate (Vector3.right * hSliderValue * Time.deltaTime);
 }
 

}


Thanks a lot!

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

Answer by hav_ngs_ru · Nov 05, 2014 at 02:38 PM

 private float hSliderValue = 0.0f;
 
 // setup this links in editor, just drag your cameras from scene
 public Camera leftCamera; 
 public Camera rightCamera;
 
 void OnGUI () {
  hSliderValue = GUI.HorizontalSlider (new Rect (25, 25, 100, 30), hSliderValue, 0.0f, 10.0f);
 }
 
 void Update() {
   //transform.Translate (Vector3.right * hSliderValue * Time.deltaTime);
   // Ive commented it because here your slider sets VELOCITY of camera movement, not POSITION
 
   //if you want to slider sets POSITION - dont translate cameras every update
   leftCamera.transform.localPosition = new Vector3(
      leftCamera.transform.localPosition.x, 
      hSliderValue, 
      leftCamera.transform.localPosition.z);
   rightCamera.transform.localPosition = new Vector3(
      leftCamera.transform.localPosition.x, 
      -hSliderValue, 
      leftCamera.transform.localPosition.z);
   // additionally you may have some multiplier for hSliderValue here  to make camera move slower or faster
 }





Comment
Add comment · Show 6 · 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 sadek9 · Nov 05, 2014 at 03:02 PM 0
Share

2 casies:

1.

//if you want to slider sets POSITION - dont translate cameras every update

leftCamera.transform.position.localPosition.y = hSliderValue;

rightCamera.transform.position.localPosition.y = -hSliderValue;

UNITY show me the error next to this lines: Element UnityEngine.Vector3 doesn't contain "localPosition" definition, and UNITy doesn't found expanding method "localPosition" which for first argument recive argument "UnityEngine.Vector3" type.

2.

//if you want to slider sets POSITION - dont translate cameras every update

leftCamera.transform.position.localPosition.y = hSliderValue;

rightCamera.transform.position.localPosition.y = -hSliderValue;

UNITY show me the error next to this lines: You can't modify return value "Unity.Engine.Transform.LocalPosition, because it is not a variable.

avatar image AlucardJay · Nov 06, 2014 at 01:43 PM 1
Share

Access either the transform position variable OR the localPosition variable, not both.

http://docs.unity3d.com/ScriptReference/Transform.html

http://docs.unity3d.com/ScriptReference/Transform-position.html

http://docs.unity3d.com/ScriptReference/Transform-localPosition.html

avatar image hav_ngs_ru · Nov 06, 2014 at 02:11 PM 0
Share

arrh, sorry! of cause transform.localPosition, not transform.position.localPosition

avatar image sadek9 · Nov 11, 2014 at 11:56 PM 0
Share

Thanks a lot for help! ;)

avatar image sadek9 · Nov 18, 2014 at 09:44 PM 0
Share

Thanks for advise ;)

Show more comments

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

Zooming camera to child objects of .obj model 0 Answers

throwing a ball camera position changes 0 Answers

I make a camera that change position every X seconds but i need different waiting time for each position that take. 0 Answers

How can camera Forward Or How to change position of camera? 1 Answer

Placing GameObject as Child in Hierarchy Changes Its Position 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