Grab.Update () (at Assets/Scripts/Grab.cs:37)
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grab : MonoBehaviour
{
[Header("Settings")]
public float GrabPower = 10;
public float ThrowPower = 50;
public float distance = 3;
[Space(10)]
public Transform offset;
private bool grabB;
private bool throwB;
[Space(10)]
public Inventory inventory;
RaycastHit hit;
bool start;
float timer = 1;
void Start()
{
inventory = FindObjectOfType<Inventory>();
}
void Update()
{
Physics.Raycast(transform.position, transform.forward, out hit, distance);
if (Input.GetKey(KeyCode.F))
{
//Ошибка приводит сюда!!
if (hit.collider.tag == "Grab")
{
inventory.Active = true;
grabB = true;
}
else
{
inventory.Active = false;
grabB = false;
}
}
else
{
inventory.Active = false;
grabB = false;
}
if (timer == 1)
{
if (grabB)
{
hit.rigidbody.velocity = (offset.position - (hit.transform.position + hit.rigidbody.centerOfMass)) * GrabPower;
if (Input.GetKey(KeyCode.Mouse0))
{
start = true;
throwB = true;
grabB = false;
}
}
}
if (throwB)
{
hit.rigidbody.velocity = transform.forward * ThrowPower;
throwB = false;
}
if (start)
{
timer -= Time.deltaTime;
}
if (timer < 0)
{
start = false;
timer = 1;
}
}
}
using System.Collections.Generic;
using UnityEngine;
public class Grab : MonoBehaviour
{
[Header("Settings")]
public float GrabPower = 10;
public float ThrowPower = 50;
public float distance = 3;
[Space(10)]
public Transform offset;
private bool grabB;
private bool throwB;
[Space(10)]
public Inventory inventory;
RaycastHit hit;
bool start;
float timer = 1;
void Start()
{
inventory = FindObjectOfType<Inventory>();
}
void Update()
{
Physics.Raycast(transform.position, transform.forward, out hit, distance);
if (Input.GetKey(KeyCode.F))
{
//Ошибка приводит сюда!!
if (hit.collider.tag == "Grab")
{
inventory.Active = true;
grabB = true;
}
else
{
inventory.Active = false;
grabB = false;
}
}
else
{
inventory.Active = false;
grabB = false;
}
if (timer == 1)
{
if (grabB)
{
hit.rigidbody.velocity = (offset.position - (hit.transform.position + hit.rigidbody.centerOfMass)) * GrabPower;
if (Input.GetKey(KeyCode.Mouse0))
{
start = true;
throwB = true;
grabB = false;
}
}
}
if (throwB)
{
hit.rigidbody.velocity = transform.forward * ThrowPower;
throwB = false;
}
if (start)
{
timer -= Time.deltaTime;
}
if (timer < 0)
{
start = false;
timer = 1;
}
}
}