- Home /
Moving the Camera around my Player
Hey guys,
i am trying to build up a game where i am moving a sphere as my Player with the Arrow-Keys.
Everything works fine!
My Problem is that i want to build a labyrinth, in which the player sees the Playersphere from behind and a little bit up. When the Player is changing directions with the Arrow-Keys the Camera should move around the object, so that the player ist always looking into the direction he is moving. Just like in Need for Speed or other similar Games, when u turn around the camera turns around, too.
Is there a posibility to do so? A tutorial or anything that can help me would be awesome.
Greets treceguete
I am sorry for my bad english, unfortunatelly its not my mothertongue...
There's a lot of ways to do it, but any it's simply. Just try with rotate around when the direction respect the camera is not foward. http://docs.unity3d.com/ScriptReference/Transform.TransformDirection.html
Answer by Digital-Phantom · Mar 20, 2015 at 12:16 PM
Just make your camera a child of the sphere. (in the hierarchy drag your main camera onto the sphere) It then becomes a child of the sphere.
Set the cameras co-ordinates (X Y Z) to 0, 0, 0 (that's dead centre of the sphere)
Now move your camera back and up a little to a position where the view is good for you.
Now the good bit... wherever you move your sphere the camera will follow, whenever you turn/rotate your sphere the camera will turn with it.
:)
Parent/child positioning/movement tutorial
This is a very basic script for a simple camera follow control.
#pragma strict
var target : Transform;
var distance : float;
function Update()
{
transform.position.z = target.position.z -distance;
transform.position.y = target.position.y+25;
transform.position.x = target.position.x;
}
Target is the transform/object that you want the camera to follow. (in your case the sphere) Put this script onto your main camera and play around with the positioning until you get the desired result.
:)
By doing this the camera would also rotate with the spehere's spin. As in through ground and such, i guess he don't want the camera to be in "total lock"
well the OP didn't state if the sphere was spinning or not, he just said it was being moved. Anyway have edited my answer to add a tutorial and a basic camera follow script for the OP
;)
Thank You. I will see what i can do. I just dont know how to integrate the PlayerSphere into the script of the camera, but i'm sure i will find a solution for that.
Hey, i am not able to integrate your code into my Program, because i dont understand most of it, Unity does not know types like var or transform.position... I am using C#.
$$anonymous$$aybe you can help me with that:
CameraController - Your Code: using UnityEngine; using System.Collections; //using pragma.strict;
public class CameraController : $$anonymous$$onoBehaviour {
public PlayerController Transform;
private float distance;
void Start()
{
distance = 10;
}
void Update()
{
//transform.position.z = PlayerController.moveHorizontal.z -distance;
transform.position.y = y+25;
//transform.position.x = PlayerController.moveVertical.x;
}
}
PlayerController - $$anonymous$$y Code:
using UnityEngine; using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour {
public Rigidbody rb;
public float speed;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed * Time.deltaTime);
}
}
I am not able to integrate the position of the Player into your Code.
Your answer
Follow this Question
Related Questions
Weapon not updating position and moving as Player moves 0 Answers
My player glitching on moving collider 0 Answers
Camera Problem 1 Answer
Unity 2D Position Issues 0 Answers
Player rotates with camera(face same direction as the camera) 1 Answer