- Home /
camera rotating with player
I am making a simple 2D platformer where you have to find all the keys to unlock a door. I am trying to make the player flip over when i move right or left and have succeeded but whit one problem.
My camera is a child of my player so when the player flips using this code:
f(Input.GetKey(KeyCode.D))
{
rigidbody2D.velocity = new Vector2 (speed, rigidbody2D.velocity.y);
transform.eulerAngles = new Vector2(0, 0);
}
else if(Input.GetKey(KeyCode.A))
{
rigidbody2D.velocity = new Vector2 (speed*-1, rigidbody2D.velocity.y);
transform.eulerAngles = new Vector2(0, 180);
}
The camera flips along with it which complete messes up the game!
How can i stop this from happening? Thanks in advance!
Answer by Dipetriantonio · Sep 07, 2014 at 01:32 PM
If you have the camera as a non child object and add a camera script like:
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour {
public GameObject target;
// Use this for initialization
void Start () {
target = GameObject.FindGameObjectWithTag("Player");
}
void Update(){
Vector3 tempPos = target.transform.position;
tempPos.x = tempPos.x + offsetX;
tempPos.y = tempPos.y + offsetY;
transform.position = tempPos;
transform.LookAt(target.transform.position);
}
The offset variables can be whatever fits your game. Then you just drag your player gameobject to the empty target slot in the camera script in the inspector when the camera is selected. You also need to select the tag "Player" for your character object.
Hope that helps!
cannot implicitly change unityengine.gameobject[] to unityengine.gameobject.
something along those lines i am really stuck
Answer by malekbakeer · Sep 07, 2014 at 01:39 PM
click on (Assets > Import Assets) Click On the Camera Scripts
and remove the camera from the player's child
then Select your Camera and click on (Components > Camera > SmoothFollow) or (Components > Add Component > Camera > SmoothFollow) and change the numbers as you like (The Higher The Less Smooth)
Good Luck :D