Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2D Sidescroller X / Y

Discussion in '2D' started by DarkNeo, Jul 10, 2014.

  1. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Hi there,

    I currently am using the following code for my side scroller. which is scrolling in x perfect, however I am trying to make it so the camera will follow the player up in the y direction slightly and then back down perfect. I have tried playing around with Mathf.Clamp but it's not giving me decent results.

    my camera has the character plugged into it.

    I hope someone can help or point me in the right direction. Thank you

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6. public class CameraFollow : MonoBehaviour {
    7.  
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         cameraZ = transform.position.z;
    12.  
    13.  
    14.     }
    15.  
    16.     float cameraZ;
    17.  
    18.  
    19.  
    20.     void Update () {
    21.  
    22.         transform.position = new Vector3(Player.position.x + 6.5f, 3, cameraZ);
    23.  
    24.  
    25.     }
    26.  
    27.  
    28.    
    29.     public Transform Player;
    30.  
    31. }
    32.  
     
  2. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    Hey DarkNeo,

    Take a look at the SmoothFollow.cs script from the Prime31 CharacterController Demo.

    And if you aren't using the Prime31 CharacterController2D yet, use it :)
    https://github.com/prime31/CharacterController2D

    I tried it this week, and it's a nice controller for 2D, i think it will do what you want in your sidescroller.
     
    DarkNeo likes this.
  3. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Cheers for the quick reply mate, I will take a look at that.

    Thank you :)

    EDIT: That stuff is really useful, thank you for that!
     
    Last edited: Jul 10, 2014