I’ve been working in unity for about a day now and I’ve been having trouble getting a script that I’m making to work as intended. Right now, it’s meant to move the camera towards the player, but I haven’t been able to figure out yet why it hasn’t been working. I probably messed up the variables but I’m still too new at this to figure it out.
Here’s the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamMov : MonoBehaviour
{
private GameObject player;
private float distance;
private float speed = 2.0f;
void Start()
{
player = GameObject.Find("Player");
}
void Update()
{
distance = Vector3.Distance (player.transform.position, transform.position);
transform.position = Vector3.MoveTowards (transform.position, player.transform.position, speed * Time.deltaTime);
}
}
What does “not working” mean? Does the camera move in the wrong direction? At the wrong speed? Does your code run, but not cause movement? Does your code not run at all?
(You can use Debug.Log to check if your code is running, and potentially check the values of certain variables or calculations to see if they’re what you expect.)
I don’t see anything obviously wrong with the way you are trying to move the camera in this code, so it’s likely the problem has something to do with how you’ve set up your objects and scripts in the Unity editor rather than with the code itself.