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 Balero · Dec 02, 2012 at 04:13 PM · camera controlscamera script

Camera Controller movement issue

Hello, I'm using unity to make a game on rails, and I wanted the camera to have a little bit of lag when my character moves to the left and right. I implemented a pretty simple one (camera only moves so fast towards where the character is).

However I found that it moves differently depending on if the character is moving to the left or the right. So if the Character moves to the right it moves how I expect. But it moves much faster if he is moving to the right, and if I have the camera updating slowly enough it lists to the left. I can't figure out why.

Any help would be great. Alternatively if anyone has any ideas/links as to how else I could implement the camera that would be appreciated. The code for the main camera controller is below.

 #pragma strict
 // player and camera
 var Play : GameObject;
 var MainCamera : GameObject;
 //Start position of camera, used for zero error  
 var startPosz : float;
 var startPosx : float;
 var startPosy : float;
 //Current position of camera
 var curPosz : float;
 var curPosy: float;
 var curPosx: float;
 //Position that the camera is moving to 
 var desiredPosz : float;
 var desiredPosy: float;
 var desiredPosx: float;
 //Speed that the camera follows the player
 var CameraSpeed: float;
 
 
 
 function Start () {
     Play = GameObject.FindGameObjectWithTag("Player");
     MainCamera = GameObject.FindGameObjectWithTag("MainCamera");
     // issue if the camera is slower than 0.8 (listing to the left)
     CameraSpeed = 0.1;
     startPosz = Play.transform.position.z;
     startPosy = Play.transform.position.y;
     startPosx = Play.transform.position.x;
     
 }
 
 function Update () {
     // desired location of the camera, z is moving forward, y is up and down, x is side to side 
     desiredPosz = Play.transform.position.z;
     desiredPosy = Play.transform.position.y;
     desiredPosx = Play.transform.position.x;
     //only x has lag at the moment  
     curPosx = MainCamera.transform.position.x;
     
     //update camera for forward movement
     if (desiredPosz != startPosz){
         transform.Translate(0.0f, 0.0f, (desiredPosz - startPosz));
         startPosz = desiredPosz;
     }
     
     //update camera for up/down (jumping, not tested should work) 
     if (desiredPosy != startPosy){
     //                  Horizontal,                  Verticle, Depth 
         transform.Translate(0.0f, (desiredPosy - startPosy), 0.0f);
         startPosy = desiredPosy;
     }    
     
     //Basic version follows pecisley 
     /*if (desiredPosx != startPosx){
     //                    Horizontal, Verticle, Depth 
         transform.Translate((desiredPosx - startPosx),0.0f, 0.0f);
         startPosx = desiredPosx;
     }*/
     
     if (desiredPosx != startPosx){
     
         if(curPosx + CameraSpeed < desiredPosx){
             curPosx = curPosx + CameraSpeed;
             
         } else if(curPosx - CameraSpeed > desiredPosx){
             curPosx = curPosx - CameraSpeed;
             
         }else{
             curPosx = desiredPosx;
             
         }
         transform.Translate((curPosx - startPosx ), 0.0f, 0.0f);
         startPosx = curPosx;
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How could i make a collision happen based on camera view ? 2 Answers

Making a camera switch in JS, how do I add more cameras? 1 Answer

Camera follow Player Script - Problem 1 Answer

How to import the object from server to unity 2 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