- Home /
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.
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);
}
}
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);
}
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.
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
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.
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
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.
Just putting the camera as a child of the player can cause so many bugs, and its much smoother to use a lerp function :)
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.
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
Follow this Question
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