0
\$\begingroup\$

i developing side Scrolling endless Game. i want to Change X Axis of camera as player moving to Right. and i almost Done That. but the problem is when move Camera to the Right then its look so stiff in Android Device not Move Smooth. That's the code I Write in my Script to move Camera when player move.

public static float offsetX;

void Update () {

    if (BirdScript.instance != null) {

        if (BirdScript.instance.isAlive) {
            MoveTheCamera ();
        }
    }

}

void MoveTheCamera() {
    Vector3 temp = transform.position;
    temp.x = BirdScript.instance.GetPositionX () + offsetX;
    transform.position = temp;

}

and in my Player Script(BirdScript.cs) i also set my player Position and also Get in CameraScript

void Awake() {

   setCameraX();

}

void setCameraX() {

    CameraScript.offsetX = (Camera.main.transform.position.x - transform.position.x) - 1f;

}

public float GetPositionX(){

    return transform.position.x;
}

what should i do to move my camera smoothly in device? thanks.....

\$\endgroup\$

3 Answers 3

4
\$\begingroup\$

use Mathf.Lerp for smooth transform For more details click here

using UnityEngine;
using System.Collections;

public class SmoothCamera2D : MonoBehaviour {

 public float dampTime = 0.15f;
 private Vector3 velocity = Vector3.zero;
 public Transform target;

 // Update is called once per frame
 void Update () 
 {
     if (target)
     {
         Vector3 point = camera.WorldToViewportPoint(target.position);
         Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z)); //(new Vector3(point.x, 0.5, 0.5));
         Vector3 destination = transform.position + delta;
         transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
     }
 }
}

Edit 2;

for a 2D Endless Runner no need to move the camera. set the camera as constant and scroll the background. it ll give player moving effect

Eg 1.

Eg 2.

Click the link and Read the Complete documentation and see video tutorial. They gave complete tutorial about 2d endless runner game

\$\endgroup\$
8
  • \$\begingroup\$ i use this,,, but no change. output will be same...in device.. \$\endgroup\$ Mar 3, 2016 at 7:17
  • 1
    \$\begingroup\$ instead of Time.time use Time.deltatime * 1.0f; \$\endgroup\$ Mar 3, 2016 at 7:36
  • \$\begingroup\$ i develop 2D Game not 3D.. i just move Camera in X Axis when Player move endless. \$\endgroup\$ Mar 3, 2016 at 7:47
  • \$\begingroup\$ i edited my post check it now \$\endgroup\$ Mar 3, 2016 at 8:00
  • \$\begingroup\$ i used this code but the problem is as it is,,, no change..... @Manoj \$\endgroup\$ Mar 3, 2016 at 9:51
0
\$\begingroup\$

There's a script called "SmoothFollow" provided by unity itself in the Standard Assets package. You should be able to modify that to make a sidescroller.

\$\endgroup\$
0
\$\begingroup\$

For 3D

If you are developing 3D game then you can get your relevant stuff from here

For 2D

If you are developing 2D game then I have a script, that will help you most probably.

using UnityEngine; using System.Collections;

public class CameraFollow2D : MonoBehaviour
{
    [Header("Camera Follow")]
    public float _dampTime = 0.15f;
    public Transform _target;

    // These public booleans allow you to move in any specific direction
    public bool _followX = true;
    public bool _followY = true;

    private Vector3 _velocity = Vector3.zero;

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {
        if (_target) {
            Vector3 point = Camera.main.WorldToViewportPoint (_target.position);
            Vector3 delta = _target.position - Camera.main.ViewportToWorldPoint (new Vector3 (0.5f, 0.5f, point.z)); //(new Vector3(0.5, 0.5, point.z));
            Vector3 destination = transform.position + delta;
            transform.position = Vector3.SmoothDamp (transform.position, new Vector3 (_followX ? destination.x : transform.position.x,
                                                                                     _followY ? destination.y : transform.position.y,
                                                                                     transform.position.z), ref _velocity, _dampTime);
        }

    }
}

In both cases, drop the script to your Camera. Assign target to it from inspector and configure it according to your need.

\$\endgroup\$
1
  • \$\begingroup\$ ya i try this type Code that's work but only unity Editor.... when i Run my Game in Android Device then Not Work Perfectly... Camera not Follow Player Smoothly.... & in unity Editor That's Work. i don't know how to solve this problem... \$\endgroup\$ Mar 4, 2016 at 9:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .