Hi I just made a 2d endless runner and this is the code i used to get the camera to follow the player (its in C#)
using UnityEngine;
using System.Collections;
public class CameraRunnerScript : MonoBehaviour {
public Transform player;
void Update ()
{
transform.position = new Vector3 (player.position.x + 6, 0, -10); // Camera follows the player but 6 to the right
}
}
it works fine following the player on the x axis but i was hoping someone could help me edit it so it follows the player on the y axis as well. thanks
Pretty easily achieved. Your code already demonstrates how to do it.
Here’s an updated version to make it a little easier for you. Just change the ‘offset’ values in the inspector to whatever you want. Something like (6, 6, -10) perhaps.
public Transform player;
public Vector3 offset;
void Update ()
{
transform.position = new Vector3 (player.position.x + offset.x, player.position.y + offset.y, offset.z); // Camera follows the player with specified offset position
}
Unity 2D doesn’t have the Cinemachine tool like Unity 3D does so your first thought might go to code, but that’s unnecessary. All you need to do to get the camera to track your player character is to make the camera object a child of the player character object. Any offset you want can easily be inputted into the transform component of your camera object after you make it a child
Hi, I am having difficulty with this as well.I am using Unity 3d 2017.1.0f3 (64 bit). I have a game object named “Player” and then as a child “Robot” (which is what is linked to animations and character controls). I made a C# script for the camera with the following code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowTarget : MonoBehaviour
{
//what we are following
public Transform target;
//zeros out velocity
Vector3 velocity = Vector3.zero;
//time to follow target
public float smoothTime = .15f;
void fixedUpdate()
{
Vector3 targetPos = target.position;
//align the camera and the target z position
targetPos.z = transform.position.z;
transform.position = Vector3.SmoothDamp (transform.position, targetPos, ref velocity, smoothTime);
}
}
The console shows no errors, and I have the Target linked to the “Robot” game object, but the camera does not move at all. I have also tried saving the scene and closing unity then reopening it, but that did not work either. I am a little confused because I don’t see a difference in the code or other parameters from the tutorial i am following along with. If it would be helpful I can provide a link to the video I am following.
//what we are following
public Transform target;
//zeros out velocity
Vector3 velocity = Vector3.zero;
//time to follow target
public float smoothTime = .15f;
// Update is called once per frame
void Update ()
{
transform.position = new Vector3(player.position.x + offset.x, player.position.y + offset.y, offset.z);
}
If some of you have the same problem as me (the camera only shows the background here is an easy fix
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class followPlayerScript : MonoBehaviour
{
public Transform playerPosition;
// Update is called once per frame
void FixedUpdate()
{
transform.position = new Vector3(playerPosition.position.x, playerPosition.position.y, transform.position.z);
}
}
It s basiclly the same thing except the coordinate of the y axis is the same as before the game updated
hope this helps someone
Now you can use a Cinemachine Virtual 2D camera.
This will probably be the easiest way to do it and the most modular, in the sense that you are not tying your camera to the player either in the editor or in code, you simply tell the Virtual camera to follow any transform in your scene, including, of course, the Player.
You will find it under Cinemachine-> Virtual Camera
Hi I’m trying to make a game of a sphere moving on 2D ground in the z-axis direction and when I try to follow the sphere with the camera it stops the movement of the ball to the right and left
Here is my code for the movement:
public Rigidbody rb;
// Use this for initialization
void Start()
{
rb = GetComponent();
rb.AddForce(Vector3.forward * 1, ForceMode.Impulse);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
rb.AddForce(Vector3.left * 1, ForceMode.Impulse);
rb.AddForce(Vector3.forward * 1, ForceMode.Impulse);
}
if (Input.GetKeyDown(KeyCode.D))
{
rb.AddForce(Vector3.right * 1, ForceMode.Impulse);
rb.AddForce(Vector3.forward * 1, ForceMode.Impulse);
}
if (Input.GetKey(KeyCode.Space))
{
Vector3 dir = new Vector3(-10f, 15f, 0f);
dir.Normalize();
this.gameObject.GetComponent<Rigidbody>().AddForce(dir * 50);
rb.AddForce(Vector3.forward * 1, ForceMode.Impulse);
}
Any help how can I can solve my problem in the z-axis direction please