0

So, i'm building a 3D runner and i've got a lot of problems making the camera following the ball. I've been on google for a few hours and can't find anything that's not outdated or fills in the things I need. I want a camera that follows my ball but goes straight in the X-axis. When there are the stairs the camera needs to follow on Y axis.

I just don't want my camera to rotate (since my object is a rolling ball) and to move in the Z-axis.

var myPos : Vector3;
var myPlay : Transform;

function Update()
{
transform.position = myPlay.position + myPos;
}

This is what I already have. It doesn't rotate, but it follows on the Z-axis. I don't want that.

http://prntscr.com/9pmypz This is what it looks like in the Inspector.

BelgianWizard
  • 99
  • 1
  • 4
  • 11
  • Please show the code for what you've tried so far – Keith M Jan 13 '16 at 16:28
  • See the answer I posted – BelgianWizard Jan 13 '16 at 16:36
  • Which version of Unity are you using? Unity 5 has unique ways of referencing objects compared to previous versions... Also, I don't think the C# tag should be applied. Yes Unity uses C# but this is a Unity specific question, not a C# question... – HDL_CinC_Dragon Jan 13 '16 at 16:43
  • @HDL_CinC_Dragon re: your suggested edit - the C# tag is questionable, but the unity tag is definitely wrong - it should be unity3d, as the unity tag is about a completely different library that even says specifically not to use it for unity3d. – GalacticCowboy Jan 13 '16 at 16:47
  • I am currently using unity 5. I'm sorry for putting the C# tag in, didn't really think about it. – BelgianWizard Jan 13 '16 at 16:47
  • @GalacticCowboy Thanks for letting me, I wasn't actually aware of that. I don't normally dabble in the Unity (or Unity3D) areas at all as I'm still learning Unity myself through the provided tutorials on Unity's site. As for the C# tag, I really don't think it should be there as even Jon Skeet (Who I'm assuming doesn't play with Unity at all) wouldn't know how to answer as this question is completely dependent on Unity's framework. – HDL_CinC_Dragon Jan 13 '16 at 16:49

2 Answers2

0

As per Unity3D's Roll-A-Ball tutorial:

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour
    {
        public GameObject player;

        private Vector3 offset;

    void Start ()
        {
            offset = transform.position - player.transform.position;
        }

    void LateUpdate ()
        {
            transform.position = player.transform.position + offset;
        }
    }
HDL_CinC_Dragon
  • 194
  • 1
  • 8
0

I can't comment @HDL_CinC_Dragon answer because of my low reputation, but I wanted to add that using the public access modifier is a really bad habit. Instead you should use the private access modifier with the SerializeField attribute on the field, like this:

public class CameraController : MonoBehaviour
    {
        [SerializeField]
        private GameObject player;

        private Vector3 offset;
Stud
  • 521
  • 4
  • 8
  • In this case, the player `GameObject` is already a public member as it's an asset within the project rather than a member of the camera controller since `player` is essentially just a hook to the public object. Am I incorrect here? (I am by far no Unity expert but I feel with this very specific case, the use of `private` and the `[SerializeField]` attribute shouldn't be necessary... Unless you're wanting the `GameObject` to show up as a parameter of the Camera object in the Inspector rather than just using `GetComponent` for defining `player`? – HDL_CinC_Dragon Jan 13 '16 at 18:38
  • This is not about private and [SerializeField] being "necessary", your attributes should always be private. You can read why in any book about OOP. I don't know how the Unity team built this project, but there is no good reason to put this variable public and you have a lot of different way to avoid that, having a serialized field in the inspector is just one of them. – Stud Jan 14 '16 at 08:57