Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Код юнити выдаёт ошибку, ни пойму в чём ошибка

да да Ученик (61), на голосовании 3 месяца назад
 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraControl : 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);
}
else
{
transform.position = new Vector3(player.position.x + offset.x, player.position.y + offset.y, transform.position);
}
}
if(player)
{
int currentX=Mathf.RoundToInt(player.position.x);
if (currentX > lastX) isLeft=false;
else (currentX < lastX) isLeft=true;
lastX = Mathf.RoundToInt(player.position.x);

Vector3 target;
if (IsLeft)
{
target = new Vector3(player.position.x - offset.x, player.position.y - offset.y, transform.position);
}
else
{
target = new Vector3(player.position.x + offset.x, player.position.y + offset.y, transform.position);
}
Vector3 currentPosition = Vector3.Lerp(transform.position, target, dumping * Time.deltaTime);
transform.position = currentPosition;
}
}
Голосование за лучший ответ
Сергей Бивис Мастер (1587) 4 месяца назад
  1. Пропущена точка с запятой после объявления переменных dumping и offset.
  2. offset должен использовать запятую вместо точки в конструкторе Vector2.
  3. Метод FindPlayer должен использовать transform вместо Transform.
  4. Условие для проверки player и соответствующий код должны быть внутри метода Update.
  5. Условие else использует синтаксис, который приведет к ошибке.
  6. IsLeft должно быть isLeft.
  7. transform.position должно учитывать координату z.

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

public class CameraControl : 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);
}

void Update()
{
if (player)
{
int currentX = Mathf.RoundToInt(player.position.x);
if (currentX > lastX) isLeft = false;
else if (currentX < lastX) isLeft = true;
lastX = 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;
}
}

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);
}
}
}
да даУченик (61) 4 месяца назад
спасибо, но пишет ошибку
UnassignedReferenceException: The variable rb of Player has not been assigned.
You probably need to assign the rb variable of the Player script in the inspector.
UnityEngine.Rigidbody2D.get_velocity () (at <04af773e223344bd9a56d36a602cfd42>:0)
Player.Update () (at Assets/Scripts/Player.cs:18)
Похожие вопросы