Problem about scripting Diy_Follow_Camera in 2018.2.7f1 Version Unity

Hello there.
I have a problem finishing the camera follow section.
You have taught on 2015 version but i think its different in 2018.
Can you help me with it?

Thanks.
(i tried lot of ways including just copying and pasteing the scp.

Hi Onur,

“2015” would be relevant only to the version of Visual Studio, Unity was versioned 4.x, 5.x, 2017.x and now 2018.x.

Perhaps if you mention the specific problem you are experiencing, add screenshots to help explain the issue, or perhaps a video and post your code, that may help get you some answers :slight_smile:


See also;

Hi Rob,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    GameObject player;
    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("player");
    }

    // Update is called once per frame
    void LateUpdate()
    {
        transform.position = player.transform.position;
    }

this is the script i used…

and this is the error i received;

Warning: no main camera found. Third person character needs a Camera tagged “MainCamera”, for camera-relative controls.
UnityEngine.Debug:LogWarning(Object, Object)
UnityStandardAssets.Characters.ThirdPerson.ThirdPersonUserControl:Start() (at Assets/Standard Assets/Characters/ThirdPersonCharacter/Scripts/ThirdPersonUserControl.cs:26)

P.S; I have my main camera tagged as a main camera below the camera arm section.
P.S 2: I dont have any screen recorder.:frowning:

Thanks…

Could you select the camera in the Hierarchy and then take a screenshot with its details on display in the Inspector please.

Looks like most of your script is missing in the example you’ve given too.


See also;


here it is…

it is your script.But when i open CameraFollow part;
İt gives me this script basically:

using System;
using UnityEngine;


namespace UnityStandardAssets._2D
{
    public class CameraFollow : MonoBehaviour
    {
        public float xMargin = 1f; // Distance in the x axis the player can move before the camera follows.
        public float yMargin = 1f; // Distance in the y axis the player can move before the camera follows.
        public float xSmooth = 8f; // How smoothly the camera catches up with it's target movement in the x axis.
        public float ySmooth = 8f; // How smoothly the camera catches up with it's target movement in the y axis.
        public Vector2 maxXAndY; // The maximum x and y coordinates the camera can have.
        public Vector2 minXAndY; // The minimum x and y coordinates the camera can have.

        private Transform m_Player; // Reference to the player's transform.


        private void Awake()
        {
            // Setting up the reference.
            m_Player = GameObject.FindGameObjectWithTag("Player").transform;
        }


        private bool CheckXMargin()
        {
            // Returns true if the distance between the camera and the player in the x axis is greater than the x margin.
            return Mathf.Abs(transform.position.x - m_Player.position.x) > xMargin;
        }


        private bool CheckYMargin()
        {
            // Returns true if the distance between the camera and the player in the y axis is greater than the y margin.
            return Mathf.Abs(transform.position.y - m_Player.position.y) > yMargin;
        }


        private void Update()
        {
            TrackPlayer();
        }


        private void TrackPlayer()
        {
            // By default the target x and y coordinates of the camera are it's current x and y coordinates.
            float targetX = transform.position.x;
            float targetY = transform.position.y;

            // If the player has moved beyond the x margin...
            if (CheckXMargin())
            {
                // ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
                targetX = Mathf.Lerp(transform.position.x, m_Player.position.x, xSmooth*Time.deltaTime);
            }

            // If the player has moved beyond the y margin...
            if (CheckYMargin())
            {
                // ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
                targetY = Mathf.Lerp(transform.position.y, m_Player.position.y, ySmooth*Time.deltaTime);
            }

            // The target x and y coordinates should not be larger than the maximum or smaller than the minimum.
            targetX = Mathf.Clamp(targetX, minXAndY.x, maxXAndY.x);
            targetY = Mathf.Clamp(targetY, minXAndY.y, maxXAndY.y);

            // Set the camera's position to the target position with the same z component.
            transform.position = new Vector3(targetX, targetY, transform.position.z);
        }
    }
}

Thanks…

A couple of things to check/try…

  • whilst it should be using the tags, might not hurt to just rename the GameObject in the Hierarchy to “MainCamera”
  • check other GameObjects to ensure they are not also tagged “MainCamera”, if you look at the documentation it states that it returns the first object it finds tagged accordingly, so, if for example your CameraArm GameObject was tagged “MainCamera” and that was found first, it may then fall over because it doesn’t have a Camera component on it.

Have a quick try/test and let me know the outcome.

Incidentally, those are two very different scripts you have posted there… one is from the Standard Assets, and I suspect the other (the first one) is part of the course.

I double checked the tags but no problem as i see…
No other cameras…
No rename based problems.(changed from untagged to main camera{the main camera/again/})
What to do now?

I’ve got about half an hour before I do the school run, if you would like to zip up your project files and share them with me I’ll take a quick look for you.

The forum will allow uploads of up to 10mb, if your project files (zipped) are larger than that, quite likely for the RPG, then you will need to use a service such as Google Drive or Dropbox and then share the URL.

I am receiving this error

UnityException: Tag: player is not defined.
CameraFollow.Start () (at Assets/Standard Assets/2D/Scripts/CameraFollow.cs:10)

after this script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour{
    GameObject player;
    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        print(player.ToString());
    }

    // Update is called once per frame
    void LateUpdate() {

    }    
}

That would be because you haven’t applied the Player tag to your player.

I’m almost out of time, did you want to share the project?

Could you also start applying the code formatting in your posts, it makes them a lot easier for everyone to read - see the link I’ve provided in my earlier responses for details - thanks :slight_smile:

Sorry im new :smiley: will correct them.ASAP.But i will work on my problem and let you know in evening…
Thank you!

But did you mean i need to tag my player in code or in unity??

Have a good day…

No worries, that’s why I gave you the links and tidied up behind you :slight_smile:

If you still need some help later, just share the project files with me and I’ll take a look :slight_smile:

Within the Unity editor, where you set the tag (like MainCamera).

Just as a heads-up, the scripts from the Standard Assets you are using have “2D” in their file path…

Thanks Rob;

I just redo the project and it solved by itself :slight_smile:

I guess i did some mistakes tagging the objects and player…

Have a good day…

1 Like

You’re very welcome and I’m glad you have resolved the issues and can move forward, we’ll done :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms