At present I am working on 2d side scrolling game. I want camera follow for player movement only in horizontal movement. Right now I have written this type of target following script Code (CSharp): public class SmoothFollow : MonoBehaviour { public Transform target; public float smoothness; private Vector3 targetInitialPos; void Awake () { targetInitialPos = target.position; } void Update () { Vector3 cameraPosition = target.position - targetInitialPos; cameraPosition.y = 0f; cameraPosition.z = -10f; transform.position = cameraPosition; // transform.position = Vector3.Lerp (transform.position , cameraPosition , Time.deltaTime * smoothness); } } Using above code it shows jerky movement of camera. Not smoother one that I want. Please give some suggestion in this.
Here is the script I use on my camera to get smoother camera movement; http://pastebin.com/D6A0pQPn To disable Y position, just change the Vector3 to ingore the Y position of the player. Make sure to add the Player to the camera script as well. Hope it helps
If your player is a rigidbody then you could try turning on Interpolation... http://docs.unity3d.com/ScriptReference/RigidbodyInterpolation.html http://docs.unity3d.com/ScriptReference/Rigidbody-interpolation.html
@Ivanskodje, Thanks for your help but I can't able to decide which value suited to my implementation. Using your script I am noticing totally wrong behaviour. I have also played with multiple values but didn't got succeed. Please help me to use your script.
Here you go; http://pastebin.com/rhyh2cJr This is a much simpler version of the first script I posted. This one will simply follow the player smoothly in the X direction. Just change the "constantYposition" to match your camera Y axis. This is the value that will not change, regardless of your character position. The camera will follow the target player (X axis), so make sure to use Unity to assign it a player transform (drag the player game object to the script so it looks something like this http://prntscr.com/6o3xug ). Hope it helps!
After using you new code it does not behave correct at all. Following image illustrate where player is moving and continuous vibrate on screen. I want camera to be 0 in vertical movement and player need to be left hand side of screen. Please help me to use your script. I can't able to figure out where I am doing wrong. I played with every damping value but didn't got success.
Are you programming in an 2D or 3D environment? Make sure X is left/right and Y is up/down in an 2D environment. I can't help any more unless you post your code / project here. :S If you don't know how to work with XY(Z) Vectors, I would highly recommend you play around with various tutorials on YouTube to learn the basics The game looks very nice though. Made from a tutorial or did you make everything yourself?
I have used following screen for my player follow Code (CSharp): using UnityEngine; using System.Collections; using System; // Add this script to the camera you want to follow the player public class CameraController : MonoBehaviour { // Player (the Transform of whatever you want the camera to follow) public Transform player; // Change this to match your game. This is the Y coordinate of where your camera will look. // It will only follow the player left and right, so make sure you position this correctly! :) public float constantYposition; // Increase to make the camera follow the player slower - or decrease for more instant gratification public float damping = 0.5f; // Used to keep track of the players previous position private Vector3 lastTargetPosition; // Sorry - I can't figure out what this is used for, as it apparently is never initialized. // However, this needs to be here so we can ref it in the SmoothDamp thing. :S private Vector3 currentVelocity; // Use this for initialization void Start () { // Save player position as last player position lastTargetPosition = player.position; // This will prevent the camera from following a parent - in case you added the camera as somethings child. // (which makes the camera follow whatever parent) transform.parent = null; } // Update is called once per frame void Update () { if (!player) { player = GameObject.FindGameObjectWithTag (GameConstants.TAG_MR_JUMP).transform; } else { // Camera follows the player smoothly with this Vector3 newPos = Vector3.SmoothDamp (new Vector3 (transform.position.x, constantYposition, transform.position.z), player.position, ref currentVelocity, damping); newPos.z = -10f; newPos.y = 0f; // Change the position to the camera transform.position = newPos; // Set player position as last camera position (helps us to give the effect of smoothly following the player) lastTargetPosition = player.position; } } } Public variable value Constant YPosition = 0 Damping = 0.5 Link for game video : https://www.dropbox.com/s/j453wltivwogj17/MrJumpCameraController.mov?dl=0 Now give me some suggestion. Where I am doing wrong? I really need your help.
Ah, I believe I understand the problem. Your "player" is twitching a bit when it reaches the maximum damping distance towards the right from the camera, correct? I actually have a similar issue, but haven't prioritized fixing it as appearance and "visual looks" is the last parts of the puzzle when creating a game (at least for my project). If this was indeed the problem, I will look into it and post here when I figure it out! I am unfortunately on a tiny vacation (I prefer relax at home and do some programming! I know, I'm weird...) and should hopefully be back by Monday to check things out (unless someone have managed to find a fix for it!) Of course if I have time, I might be able to find a fix sooner, but no promises!
@Ivanskodje , Thanks for your suggestions. I want camera follow smoothness. No problem if you reply me on monday. Enjoy your vacation But I am waiting for your reply. Please give me some suggestion in this.
Hi @siddharth3322 , A easy way to achieve what you want: Code (CSharp): using UnityEngine; using System.Collections; public class CameraJucator : MonoBehaviour { public int yOffset = 5; private Transform player; void Awake() { player = GameObject.FindGameObjectWithTag("Player").transform; } void Update () { transform.position = new Vector3(player.position.x + 7, player.position.y + yOffset, -30); } } *And this is my personal script that I use, more advanced : Code (CSharp): using UnityEngine; using System.Collections; /************************ * CopyRight 2015 * Buraca Dorin ***********************/ public class CameraJucator : MonoBehaviour { public Camera cameraP; //MAIN CAMERA public Transform jucator; //PLAYER public float compesatieX = 7; //OFFESET ON X public float marimeCamera = 6f; //CAMERA SIZE (ORTOGRAPHIC) public float vitezaCam = 2f; //CAMERA SPEED public float marimeMaxima = 7.5f; //MAX CAMERA SIZE public float marimeMinima = 5.5f; //MIN CAMERA SIZE void Update() { marimeCamera = 2f + jucator.transform.position.y; if (marimeCamera >= marimeMaxima) { marimeCamera = marimeMaxima; } if (marimeCamera <= marimeMinima) { marimeCamera = marimeMinima; } transform.position = new Vector3(jucator.position.x + compesatieX, 4.5f, -50); cameraP.orthographicSize = Mathf.Lerp(cameraP.orthographicSize, marimeCamera, Time.deltaTime / vitezaCam); } } *I will repost the script in english to make'it more understandable Hope this will help you Cheers,
@D0R1N , Thanks for your reply. But as per my consideration if I directly apply vector to camera tranform generated jerky behaviour. One more thing for my game I don't need orthographic size change during follow.
I have found a fix that works for me; First go to Project Settings -> Time Then reduce the Fixed Timestep to 0.01 (previously 0.02). Its super smooth now!
Hey siddharth3322, Make sure your Player is on the FixedUpdate() function if you are controlling it with physics. After that move the Camera Follow code to LateUpdate(). Read here: http://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html Code (CSharp): using UnityEngine; using System.Collections; public class CameraController : MonoBehaviour { public Transform target; Vector3 refVelocity = Vector3.zero; // Use this for initialization void Start () { } // Update is called once per frame void LateUpdate () { transform.position = Vector3.SmoothDamp(transform.position, new Vector3(target.position.x, 0, -10f ), ref refVelocity, 0.2f); } }
Sweet! I knew there had to be something better than what I used. Learn something new every day Hope it works for siddharth too!
Thanks friends, After reading all above details I am pretty sure I can able to solve this problem. I will reply you in short time because right now I am working on some other project.
As per my experience for smooth movement following changes work from my side Fixed Time Step : 0.02 Maximum Allowed Time : 0.02 in Time Setting Inspector. Because there is glitch from rendering part not from physics part. So speed up value to this can change game behaviour.