Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Slower camera move from side to side

Discussion in '2D' started by SVKsuli, Apr 7, 2017.

  1. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    Hello i need help with my little problem, i know it was solved in many forum posts but i still dont know how to do it right in my situation, here is code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FollowPlayer : MonoBehaviour {
    6.  
    7.     public GameObject player;
    8.     public bool delay;
    9.     public float side = 1;
    10.     float t;
    11.  
    12.     void Start()
    13.     {
    14.  
    15.     }
    16.  
    17.     void Update () {
    18.         t += Time.deltaTime;
    19.  
    20.         if (delay) //using for background
    21.             transform.position = new Vector3((player.transform.position.x - (player.transform.position.x * 0.1f)),
    22.                                             ((player.transform.position.y + 5) - ((player.transform.position.y + 5) * 0.1f)),
    23.                                             -9);
    24.         else
    25.         {
    26.             if (player.GetComponent<PlayerControl>().speed > 0) //moving to right
    27.             {
    28.                 side = 5;
    29.                 //side = Mathf.Lerp(player.transform.position.x, player.transform.position.x + 5, t);
    30.                
    31.             }
    32.             else if (player.GetComponent<PlayerControl>().speed < 0) //moving to left
    33.             {
    34.                 side = -5;
    35.                 //side = Mathf.Lerp(player.transform.position.x, player.transform.position.x + -5, t);
    36.                
    37.             }
    38.  
    39.             transform.position = new Vector3(player.transform.position.x + side, player.transform.position.y + 2, -10);
    40.         }
    41.     }
    42. }
    43.  
    Its a 2d platformer game and i want to change camera more to right side when player move to right and when he change moving to left change camera position more to left side, that was a player at the end of the screen... this code work how i want but camera position is changed immediately and i want to slow it down, i try to use mathf.lerp but i dont know how to use it in this situation.
    I try to use Vector3.lerp but it create an efect when player was moving then camera has delay and that i dont want.
     
  2. 1Piotrek1

    1Piotrek1

    Joined:
    Mar 14, 2014
    Posts:
    130
    Code (CSharp):
    1. void Start()
    2. {
    3.     playerControl = player.GetComponent<PlayerControl>();//You shouldn't use GetComponent in each frame, it is very heavy function
    4. }
    5.     void Update()
    6. {
    7.     t += Time.deltaTime;
    8.  
    9.     if (delay) //using for background
    10.         transform.position = new Vector3((player.transform.position.x - (player.transform.position.x * 0.1f)),
    11.                                         ((player.transform.position.y + 5) - ((player.transform.position.y + 5) * 0.1f)),
    12.                                         -9);
    13.     else
    14.     {
    15.         if (playerControl.speed > 0) //moving to right
    16.         {
    17.             side = Mathf.Lerp(transform.position.x, player.transform.position.x + 5, t);//You are moving from current transform position, to player
    18.  
    19.         }
    20.         else if (playerControl.speed < 0) //moving to left
    21.         {
    22.             side = Mathf.Lerp(transform.position.x, player.transform.position.x + -5, t);
    23.  
    24.         }
    25.  
    26.         transform.position = new Vector3(side, player.transform.position.y + 2, -10);
    27.     }
    28. }
    29. }
     
  3. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    Thx for warning, I had not thought about, get component is that heavy function.
    But that what you write work on first call, but you need reset "t" to 0, because it go faster and faster, or so it worked like that when i try it... But never mind, i got it...
    I create empty object that only follow player position and its parent object of camera and background, then i create script for each individual:

    Code (CSharp):
    1. public class CameraHelpObj : MonoBehaviour { //Parent object
    2.  
    3.     public GameObject player;
    4.  
    5.     void Update () {
    6.         transform.position = player.transform.position;
    7.     }
    8. }
    Code (CSharp):
    1. public class FollowPlayer : MonoBehaviour { //Camera control
    2.  
    3.     public GameObject player;
    4.     Vector3 camPos = new Vector3(5, 2, -10);
    5.  
    6.     public bool delay;
    7.     public float side = 1;
    8.     float t;
    9.  
    10.    void Update()
    11.     {
    12.         if (player.GetComponent<PlayerControl>().speed > 0)
    13.         {
    14.             camPos = new Vector3(5, 2, -10);
    15.         }
    16.         else if (player.GetComponent<PlayerControl>().speed < 0)
    17.         {
    18.             camPos = new Vector3(-5, 2, -10);
    19.         }
    20.  
    21.         transform.localPosition = Vector3.Lerp(transform.localPosition, camPos, 0.025f);
    22.     }
    23. }
    Code (CSharp):
    1. public class BcgFollowPlayer : MonoBehaviour { //Background control
    2.  
    3.     public GameObject player;
    4.    
    5.     void Update () {
    6.         transform.position = new Vector3((player.transform.position.x - (player.transform.position.x * 0.1f)),
    7.                                             ((player.transform.position.y + 5) - ((player.transform.position.y + 5) * 0.1f)),
    8.                                             -9);
    9.     }
    10. }
    11.  
    This work how i want :)