- Home /
Question by
awesomeowen · Sep 29, 2018 at 11:00 AM ·
cameraplayer movementcamera movement
How do I make my cameras parent follow my player?
Hello I am trying to add camera shake but to do this I need to make the camera a child of an empty object but my camera follows my player.How do I make the cameras parent follow the player ?
This is the code for the scrolling(Scrolling Script):
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
/// <summary>
/// Parallax scrolling script that should be assigned to a layer
/// </summary>
public class ScrollingScript : MonoBehaviour
{
/// <summary>
/// Scrolling speed
/// </summary>
public Vector2 speed = new Vector2(10, 10);
public bool updateOn = true;
public Vector2 speedDeath = new Vector2(0, 0);
/// <summary>
/// Moving direction
/// </summary>
public Vector2 direction = new Vector2(-1, 0);
public Vector2 directionDeath=new Vector2(0,0);
/// <summary>
/// Movement should be applied to camera
/// </summary>
public bool isLinkedToCamera = false;
/// <summary>
/// 1 - Background is infinite
/// </summary>
public bool isLooping = false;
/// <summary>
/// 2 - List of children with a renderer.
/// </summary>
private List<SpriteRenderer> backgroundPart;
public void death(){
updateOn = false;
}
// 3 - Get all the children
void Start()
{
// For infinite background only
if (isLooping) {
// Get all the children of the layer with a renderer
backgroundPart = new List<SpriteRenderer> ();
for (int i = 0; i < transform.childCount; i++) {
Transform child = transform.GetChild (i);
SpriteRenderer r = child.GetComponent<SpriteRenderer> ();
// Add only the visible children
if (r != null) {
backgroundPart.Add (r);
}
}
// Sort by position.
// Note: Get the children from left to right.
// We would need to add a few conditions to handle
// all the possible scrolling directions.
backgroundPart = backgroundPart.OrderBy (
t => t.transform.position.x
).ToList ();
}
}
void Update()
{
if (updateOn == true) {
// Movement
Vector3 movement = new Vector3 (
speed.x * direction.x,
speed.y * direction.y,
0);
movement *= Time.deltaTime;
transform.Translate (movement);
// Move the camera
if (isLinkedToCamera) {
Camera.main.transform.Translate (movement);
}
// 4 - Loop
if (isLooping) {
// Get the first object.
// The list is ordered from left (x position) to right.
SpriteRenderer firstChild = backgroundPart.FirstOrDefault ();
if (firstChild != null) {
// Check if the child is already (partly) before the camera.
// We test the position first because the IsVisibleFrom
// method is a bit heavier to execute.
if (firstChild.transform.position.x < Camera.main.transform.position.x) {
// If the child is already on the left of the camera,
// we test if it's completely outside and needs to be
// recycled.
if (firstChild.IsVisibleFrom (Camera.main) == false) {
// Get the last child position.
SpriteRenderer lastChild = backgroundPart.LastOrDefault ();
Vector3 lastPosition = lastChild.transform.position;
Vector3 lastSize = (lastChild.bounds.max - lastChild.bounds.min);
// Set the position of the recyled one to be AFTER
// the last child.
// Note: Only work for horizontal scrolling currently.
firstChild.transform.position = new Vector3 (lastPosition.x + lastSize.x, firstChild.transform.position.y, firstChild.transform.position.z);
// Set the recycled child to the last position
// of the backgroundPart list.
backgroundPart.Remove (firstChild);
backgroundPart.Add (firstChild);
}
}
}
}
}
}
}
This is what I have in Unity:
screen-shot-2018-09-29-at-125653.png
(516.1 kB)
Comment
Public float offset;
Update(){ Camerasparent.transform.position = -player.transform.forward + offset;} Along those lines.
Your answer
![](https://koobas.hobune.stream/wayback/20220612175443im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody Controller move forward in the direction of the camera 0 Answers
Camera relative rigidbody third person player movement 1 Answer
Camera is moving strangely 2 Answers
Need help for player rotation and camera rotation 0 Answers
Creating a file that records the player position during a game in xyz coordinates 0 Answers