Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DullDung · Apr 22, 2018 at 10:25 AM · movementcamera-movementplayer movement

how to make camera follow player after a delay

So, basically I want the camera to be steady. Then the player like "walks into the frame", and then the camera should follow him from that point on. So, I want the camer ato follow the player after a dealy, or period of time. I found this script in a different thread, but when I apply it to my camera and apply my "Player" to "player", the camera just jumps to the player after 0.5 seconds, and that repeats itself over and over again. So I want the camera to jump to the player after like 2 seconds, and then follow him constantly.

This is the script from the other thread:

 public class CameraController : MonoBehaviour 
 {
      public GameObject player;
      private Vector3 offset;
      public float delay = 0.5f;
      private float followNow;
  
      void Start()
      {
          followNow = Time.time + delay;
          offset = transform.position;
      }
  
      void LateUpdate ()
      {
          if (Time.time > followNow) {
              if (player != null) {
                  transform.position = player.transform.position + offset;
              }
              followNow += delay;
          }
      }
  }

How can I achieve my goal?

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

Answer by Hellium · Apr 22, 2018 at 11:30 AM

 using UnityEngine;
 using System.Collections.Generic;
 
 public class DelayedCameraFollower : MonoBehaviour 
 {
     private struct PointInSpace
     {
         public Vector3 Position ;
         public float Time ;
     }
     
     [SerializeField]
     [Tooltip("The transform to follow")]
     private Transform target;
      
     [SerializeField]
     [Tooltip("The offset between the target and the camera")]
     private Vector3 offset;
     
     [Tooltip("The delay before the camera starts to follow the target")]
     [SerializeField]
     private float delay = 0.5f;
      
     [SerializeField]
     [Tooltip("The speed used in the lerp function when the camera follows the target")]
     private float speed = 5;
      
     ///<summary>
     /// Contains the positions of the target for the last X seconds
     ///</summary>
     private Queue<PointInSpace> pointsInSpace = new Queue<PointInSpace>();
  
     void LateUpdate ()
     {
         // Add the current target position to the list of positions
         pointsInSpace.Enqueue( new PointInSpace() { Position = target.position, Time = Time.time } ) ;
         
         // Move the camera to the position of the target X seconds ago 
         while( pointsInSpace.Count > 0 && pointsInSpace.Peek().Time <= Time.time - delay + Mathf.Epsilon )
         {
             transform.position = Vector3.Lerp( transform.position, pointsInSpace.Dequeue().Position + offset, Time.deltaTime * speed);
         }
     }
 }
Comment
Add comment · Show 13 · 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 DullDung · Apr 22, 2018 at 01:54 PM 0
Share

That works! BUT, now when the camera follows the player, the player like "glitches". It wobbles around and the higher the speed, the more it wobbles.

avatar image Hellium DullDung · Apr 22, 2018 at 02:08 PM 0
Share

It's hard to diagnose the problem without the player script.

avatar image DullDung Hellium · Apr 22, 2018 at 02:09 PM 0
Share

I only have a little bit of code for the movement, since the player only moves left and right

 using UnityEngine;
 
 public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour 
 {
 
     public Rigidbody rb;
     public float FloatForce = 600f;
     public float LeftRightForce = 1000f;
     // Update is called once per frame
     void FixedUpdate () 
     {
         rb.AddForce(0, 0, FloatForce * Time.deltaTime);
 
         if(Input.Get$$anonymous$$ey ("d"))
         {
             rb.AddForce(LeftRightForce * Time.deltaTime, 0, 0);
         }
         if (Input.Get$$anonymous$$ey("a"))
         {
             rb.AddForce(-LeftRightForce * Time.deltaTime, 0, 0);
         }
     }
 }
 
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

181 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

Related Questions

How to have your player controls change while your camera rotates? 0 Answers

SteamVR Movement based on controller direction 0 Answers

Mouvement issue with mouse input. 0 Answers

How to move 3 players at the same time in V form,How to move 3 characters in the same time in V form 0 Answers

I have two vectors, and I want to combine them 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