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 davidc · Feb 06, 2014 at 07:34 AM · c#2dlerpvector

Vector2 Lerp. Probably a simple solution C#

Hey Guys, Im Sorry if this is a silly question and thank you so much for having a look to see if you can help me out.

i'm trying to create a very simple 2D runner iOS game, at the moment i have a Player whom runs to the right of the screen continuously at the "speed" Variable... and for debugging i created a camera that moved to the right at the same speed, Obviously this is not ideal as the player might run away from the camera if there is any lag or hieriechy when the scripts load.

So i'm trying to get the Camera to constantly follow the player and never get too far way. Ive never used any lerps before and im not sure if they are the way to go or not but here is what i was thinking.

 using UnityEngine;
 using System.Collections;
 
 public class MainCamera : MonoBehaviour {
     
     public Transform Player;
     public int CameraSpeed = 5;
     private Vector2 Currentposition;
     private Vector2 EndPosition;
 
 
     void Start() {
         Currentposition = transform.position;
         EndPosition = Player.transform.position;
     }
 
     // Update is called once per frame
     void FixedUpdate () {
         transform.position = Vector2.Lerp (Currentposition, EndPosition, 1.0f * Time.fixedDeltaTime);
         //transform.Translate (Vector2.right * Time.deltaTime * CameraSpeed);
 
     }
 }


Currently the Camera moves to a position at the start then just stays there. Like i said, sorry if its a stupid question, if you can offer any assistance that would be much appreciated, thankyou. :)

Note: Im also planning on implementing Zoomed in / Zoomed out position variables.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by zee_ola05 · Feb 06, 2014 at 07:44 AM

I have this script attached to my Camera to follow my target. You can tweak it a little to achieve your desired result.

 public class Follow : MonoBehaviour
 {
     public GameObject target;
 
     // Update is called once per frame
     void FixedUpdate()
     {
         Vector2 pos = Vector2.Lerp ((Vector2)transform.position, (Vector2)target.transform.position, Time.fixedDeltaTime);
         transform.position = new Vector3(pos.x, pos.y, transform.position.y);
     }
 }
Comment
Add comment · 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
1

Answer by robertbu · Feb 06, 2014 at 02:57 PM

Yes, to only follow the 'x' position:

 void FixedUpdate () {
    Vector3 pos = transform.position;
    pos.x = Player.position.x;
    transform.position = Vector3.Lerp (transform.position, pos, CameraSpeed * Time.fixedDeltaTime);
  
 }
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 robertbu · Feb 06, 2014 at 07:42 AM 1
Share

There are a number of open question (to me) here. I'd need to understand a bit more about your setup to give you a complete solution, but to get your Lerp() working do:

Change the type of cameraSpeed to a float (not strictly necessary):

 public float cameraSpeed = 5.0f;

Change your Lerp() as follows:

 transform.position = Vector2.Lerp (transform.position, Player.position, cameraSpeed * Time.fixedDeltaTime);

And you can kill everything in the Start() function.

avatar image davidc robertbu · Feb 06, 2014 at 10:16 AM 0
Share

Thankyou for the help, my code is now:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ainCamera : $$anonymous$$onoBehaviour {
     
     public Transform Player;
     public float CameraSpeed = 5.0f;
     
     // Update is called once per frame
     void FixedUpdate () {
         transform.position = Vector3.Lerp (transform.position, Player.position, CameraSpeed * Time.fixedDeltaTime);
 
     }
 }

And it works fine but is there any way to get the camera to only follow the X position of the player as it currently changes the camera position to a position where you cannot see the player. Thankyou so much though

avatar image zee_ola05 davidc · Feb 06, 2014 at 10:40 AM 1
Share

Can you rephrase this: And it works fine but is there any way to get the camera to only follow the X position of the player as it currently changes the camera position to a position where you cannot see the player. Thankyou so much though

I can't understand.

Show more comments
avatar image davidc · Feb 07, 2014 at 03:44 AM 0
Share

Thankyou so much, Works perfectly now.

Just in case anyone is interested here is my finished fixed update

     void FixedUpdate () {
         Vector3 DesiredCameraPosition = transform.position;
         DesiredCameraPosition.x = Player.position.x - -9;
         transform.position = Vector3.Lerp (transform.position, DesiredCameraPosition, CameraSpeed * Time.fixedDeltaTime);
     }
 }

Note that i also added a - -9, so that the player is to the left side of the screen. Someone correct me if this is bad practice but for now it seems to be working fine.

Robertbu, if you change your comment to a answer i will flag it as answered. :) happy days

avatar image
0

Answer by lvictorino · Feb 06, 2014 at 07:43 AM

Hi,

When you say

So i'm trying to get the Camera to constantly follow the player

I answer "Why don't you attach the camera to the player hierarchy?". Then moving the player will actually move the camera. The camera will ALWAYS follow the player.

If you need some introduction camera moves (like a scrolling view of the level) you just have to attach the camera to the player hierarchy at the end of the first animation using the parent property. Something like : Camera.main.transform.parent = my_player.transform;

You still can use Vector2.Lerp (if you really want to) on the camera localPosition to add some camera effects during the run.

I hope it helps.

Comment
Add comment · Show 3 · 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 NickP_2 · Feb 06, 2014 at 07:45 AM 0
Share

Just putting the camera as a child of the player can cause so many bugs, and its much smoother to use a lerp function :)

avatar image lvictorino · Feb 06, 2014 at 08:31 AM 0
Share

Hey NickP_2, I'm curious about the bugs that can occurs with a camera in the player hierarchy. In a 3D context I can imagine the problem, but in a 2D context... can you tell me more? thx.

avatar image NickP_2 · Feb 06, 2014 at 11:12 AM 0
Share

Well I might be wrong with the word "bugs", But I had some issues in the past, when my player had to "teleport" to somewhere, the camera got stuck or didn't follow the player. This might be a unity bug, I don't know, It might be fixed, I also don't know, but I know I had some issues :P. $$anonymous$$y $$anonymous$$cher always adviced me to use a script to follow to player :)

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

22 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

Related Questions

Overriding default Vector3 to Vector2 cast? 2 Answers

How do I move my object slowly from its original position? 2 Answers

Simple script to follow player only shows the camera background 1 Answer

I need a script in C# which moves an object in only one direction at a time. 1 Answer

Distribute terrain in zones 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