Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 Dynastray · Feb 21 at 11:24 PM · unity 5unity 2dcamera-movementlerplerping

I want to lerp between different changing values

I have various game objects with a boxColliderTrigger (camTargetCollider) and these check if the player is walking into it. Those gameObjects have different locations and i want to move the camera to the position if the player enters the boxColliderTrigger. But the camera is not lerping and snaps to the next location without any smoothness.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FixedRoomCamera : MonoBehaviour
 {
 public Transform Kamera;
     public Collider2D camTargetCollider;
     
     public float accum = 0.0f;
     public Vector3 p1, p2;
 
     void OnTriggerEnter2D(Collider2D other) 
     {
         if(other.gameObject.tag.Equals("Player"))
         {
             Debug.Log("Trigger2" + p1 + p2);
             p1 = Kamera.position;
             p2 = camTargetCollider.bounds.center + new Vector3(0,0,-10);
             Kamera.transform.position = Vector3.Lerp(p1,p2, Mathf.SmoothStep(0,1,accum));
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         accum += 1.1f * Time.deltaTime;
     }
     void Awake()
     {
         p1 = new Vector3(0,0,-10);
         p2 = new Vector3(0,0,-10);
     }

}

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
1
Best Answer

Answer by Hellium · Feb 21 at 11:29 PM

  public Transform Kamera;
  public Collider2D camTargetCollider;
  
  private float accum = 0.0f;
  private Vector3 p1, p2;
  void OnTriggerEnter2D(Collider2D other) 
  {
      if(other.gameObject.tag.Equals("Player"))
      {
          accum = 0;
          p2 = camTargetCollider.bounds.center + new Vector3(0,0,-10);
      }
  }
 
  void Update()
  {
      accum = Mathf.Clamp01(accum + 1.1f * Time.deltaTime);
      Kamera.position = Vector3.Lerp(p1,p2, Mathf.SmoothStep(0, 1, accum));
  }
  void Awake()
  {
      p1 = p2 = Kamera.position;
  }


OR


  public Transform Kamera;
  public Collider2D camTargetCollider;
  [Min(0.001f)] public float transitionDuration = 0.9f;

  IEnumerator OnTriggerEnter2D(Collider2D other) 
  {
      if(other.gameObject.tag.Equals("Player"))
      {
          Vector3 origin = Kamera.position;
          Vector3 destination = camTargetCollider.bounds.center + new Vector3(0,0,-10);
          for(float t = 0 ; t < transitionDuration ; t += Time.deltaTime)
          {
              Kamera.position = Vector3.Lerp(origin, destination, Mathf.SmoothStep(0, 1, t / transitionDuration));
              yield return null;
          }
          Kamera.position = destination;
      }
  }
 
  void Update()
  {
      // Method can be removed
  }
  void Awake()
  {
      // Method can be removed
  }
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 Dynastray · Feb 22 at 12:16 AM 0
Share

The Second one did the thing, thank you very much @Hellium for you help.

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

251 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 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 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 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

Why cant any operations be performed in camera when loaded from the previous scene? 0 Answers

Need help rotating the inside of a cube correctly (lerp gets me mid-way there) 2 Answers

why Lagging from tilemap 0 Answers

character shaking when it moves 0 Answers

What are the best practices when importing 2D pixel sprites 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