@Saxerdo

Не понимаю как решить эту проблему в ошибке CS1061, для движения камеры на Юнити за игроком?

Вот код и ошибка полностью
Assets\Graphs\CameraMove.cs(26,16): error CS1061: 'Transform' does not contain a definition for 'GameObject' and no accessible extension method 'GameObject' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)

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

public class CameraMove : MonoBehaviour
{

public float dumping = 1.5f;
public Vector2 offset = new Vector2(2f, 1f);
public bool isLeft;
private Transform player;
private int lastX;



void Start()
{

offset = new Vector2(Mathf.Abs(offset.x), offset.y);
FindPlayer(isLeft);

}

public void FindPlayer(bool playerIsLeft)
{
player.GameObject.FindGameObjectWithTag("Player").transform;
lastX = Mathf.RoundToInt(player.position.x);
if (playerIsLeft)
{
transform.position = new Vector3(player.position.x - offset.x, player.position.y + offset.y, transform.position.z);
}
else
{
transform.position = new Vector3(player.position.x + offset.x, player.position.y + offset.y, transform.position.z);
}

}

void Update()
{
if (player)
{
int currentX = Mathf.RoundToInt(player.position.x);
if (currentX > lastX) isLeft = false; else if (currentX < lastX) isLeft = true;
currentX = Mathf.RoundToInt(player.position.x);

Vector3 target;
if (isLeft)
{
target = new Vector3(player.position.x - offset.x, player.position.y + offset.y, transform.position.z);
}
else
{
target = new Vector3(player.position.x + offset.x, player.position.y + offset.y, transform.position.z);
}

Vector3 currentPosition = Vector3.Lerp(transform.position, target, dumping * Time.deltaTime);
transform.position = currentPosition;
}




}
}
  • Вопрос задан
  • 179 просмотров
Пригласить эксперта
Ответы на вопрос 2
GavriKos
@GavriKos Куратор тега Unity
Вы в блокноте пишете или в нормальной IDE?
Откуда вот это:
player.GameObject
?
Вам же написали - нет такого поля GameObject. Хотя бы автодополнение откройте и посмотрите какие есть поля и методы
Ответ написан
Комментировать
Grayfox90
@Grayfox90
Пушистый дядь
Ошибка связана с тем, что вы пытаетесь использовать метод GameObject() на объекте типа Transform, который не имеет такого метода.

В строке кода player.GameObject.FindGameObjectWithTag("Player").transform; вы пытаетесь найти игровой объект с тегом "Player" и затем получить его Transform, но это неверный способ.

Вам нужно сначала получить GameObject с помощью GameObject.FindGameObjectWithTag("Player"), а затем получить его Transform с помощью GetComponent(). То есть строка должна быть изменена на:

player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();


Таким образом, вы получите Transform игрового объекта, который имеет тег "Player".
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы