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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by beginnerOlle · Dec 16, 2013 at 09:19 PM · camera2dplayer movementglitch

2d camera script, strange glitches appears

Hi guys I have written a camera script which is working pretty much the way I want it to. However, glitches that seems strange to me appears when my character moves. What the script does i basically to move the camera between two positions, depending on which direction the player is moving in but also leaving a distance for the player to move in without camera movement. The errors that happens is that sometimes the script doesn't respond immediatly so that the camera stays in the "wrong" position for a while before it finds the right spot again. I'm guessing the error is caused by some less than optimal solution I chosen. However, I've been trying hard to find out what it is but so far no luck. Any one with more experience who might see what could cause the glitches?

 using UnityEngine;
 using System.Collections;
 
 public class Kameraflytt : MonoBehaviour
 {
     GameObject Spelaren;
     float vikelseposx = 1.5f;
     float avstand = 1.5f;
     float minX = 1.3f;
     float maxX = 13.6f;
     float targetposx;
     float kam_avstand;
     bool pos1 = true;
     bool pos2 = false;
 
 
         
         void Start ()
         {
         Spelaren = GameObject.Find ("Spelaren");
         }
     
         
         void LateUpdate ()
         {
         kam_avstand = transform.position.x-Spelaren.transform.position.x;
 
                 if (kam_avstand < -(vikelseposx + avstand) && pos2) 
                 StartCoroutine ("TillPos1");
 
                 if(Input.GetAxis ("Horizontal")>0 && pos1 && transform.position.x == Spelaren.transform.position.x + avstand)
                 transform.position = new Vector3 (Mathf.Clamp (Spelaren.transform.position.x + avstand,minX,maxX), 4f, -1f);
                 
                 if(kam_avstand > avstand + vikelseposx && pos1)
                 StartCoroutine ("TillPos2");
                 
                 if(Input.GetAxis ("Horizontal")<0 && pos2 && transform.position.x == Spelaren.transform.position.x - avstand)
                 transform.position = new Vector3 (Mathf.Clamp (Spelaren.transform.position.x - avstand,minX,maxX), 4f, -1f);
             }
 
 
     IEnumerator TillPos1 () {
 
         float time = 3f;
         float i = 0.0f;
         float rate = 1.0f / time;
 
         while (i < 1.0) {
             i += Time.deltaTime * rate;
             targetposx = Mathf.Lerp (transform.position.x, Spelaren.transform.position.x + avstand, i);
             transform.position = new Vector3 (Mathf.Clamp (targetposx,minX,maxX), 4f, -1f);
             yield return null; 
         }
         pos2 = false;
         pos1 = true;
 
     }
     IEnumerator TillPos2 () {
         
         float time = 3f;
         float i = 0.0f;
         float rate = 1.0f / time;
         
         while (i < 1.0) {
             i += Time.deltaTime * rate;
             targetposx = Mathf.Lerp (transform.position.x, Spelaren.transform.position.x - avstand, i);
             transform.position = new Vector3 (Mathf.Clamp (targetposx,minX,maxX), 4f, -1f);
             yield return null; 
         }
         pos1 = false;
         pos2 = true;
         
     }
 
 
 }
 
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

17 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

Related Questions

GameObjects don't render every few frames 1 Answer

How can I fix this screen wrapping? 1 Answer

Looking for picture in picture effect using Canvas Elements and Cinemachine follow target 0 Answers

Camera relative rigidbody third person player movement 1 Answer

Moving Camera With 2 Players 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