public BuildingSO[] buildingsSO;
buildingSystem = GameObject.FindGameObjectWithTag("GridBuildingSystem");
gridBuildingSystem = buildingSystem.GetComponent<GridBuildingSystem>();
private void CreateBuildingOnLoad(string buildingName, int xPosition, int zPosition)
{
// Работает.
if (buildingName == "Bld_Barn_01")
{
building = gridBuildingSystem.buildingsSO[0];
CreateBuilding(xPosition, zPosition, building);
}
else if (buildingName == "Bld_Barn_02")
{
building = gridBuildingSystem.buildingsSO[1];
CreateBuilding(xPosition, zPosition, building);
}
else if (buildingName == "Bld_Barn_03")
{
building = gridBuildingSystem.buildingsSO[2];
CreateBuilding(xPosition, zPosition, building);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour
{
private Rigidbody rb;
[SerializeField] private Transform frontRightWheelTransform;
[SerializeField] private Transform frontLeftWheelTransform;
[SerializeField] private Transform rearRightWheelTransform;
[SerializeField] private Transform rearLeftWheelTransform;
[SerializeField] private WheelCollider frontRightWheelCollider;
[SerializeField] private WheelCollider frontLeftWheelCollider;
[SerializeField] private WheelCollider rearRightWheelCollider;
[SerializeField] private WheelCollider rearLeftWheelCollider;
[SerializeField] private AudioSource engineAudioSourse;
[SerializeField] private int motorTorque;
[SerializeField] private int steerAngle;
private float currentMotorTorque;
private float currentSteerAngle;
private float horizontalAxis;
private float verticalAxis;
private bool ButtonForwardIsPressed;
private bool ButtonBackwardIsPressed;
private bool ButtonRightIsPressed;
private bool ButtonLeftIsPressed;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
GetAxises();
HandleMotor();
HandleSteerAngle();
EngineHandler();
}
private void GetAxises()
{
//horizontalAxis = Input.GetAxis("Horizontal");
//verticalAxis = Input.GetAxis("Vertical");
verticalAxis = rb.transform.position.z;
horizontalAxis = rb.transform.position.x;
}
private void HandleMotor()
{
//currentMotorTorque = motorTorque * verticalAxis;
//frontRightWheelCollider.motorTorque = currentMotorTorque;
//frontLeftWheelCollider.motorTorque = currentMotorTorque;
//rearRightWheelCollider.motorTorque = currentMotorTorque;
//rearLeftWheelCollider.motorTorque = currentMotorTorque;
if (ButtonForwardIsPressed == true)
{
currentMotorTorque = motorTorque * verticalAxis * -1;
frontRightWheelCollider.motorTorque = currentMotorTorque;
frontLeftWheelCollider.motorTorque = currentMotorTorque;
rearRightWheelCollider.motorTorque = currentMotorTorque;
rearLeftWheelCollider.motorTorque = currentMotorTorque;
}
else
{
return;
}
if (ButtonBackwardIsPressed == true)
{
currentMotorTorque = motorTorque * verticalAxis;
frontRightWheelCollider.motorTorque = currentMotorTorque;
frontLeftWheelCollider.motorTorque = currentMotorTorque;
rearRightWheelCollider.motorTorque = currentMotorTorque;
rearLeftWheelCollider.motorTorque = currentMotorTorque;
}
else
{
return;
}
}
private void HandleSteerAngle()
{
//currentSteerAngle = steerAngle * horizontalAxis;
//frontRightWheelCollider.steerAngle = currentSteerAngle;
//frontLeftWheelCollider.steerAngle = currentSteerAngle;
if (ButtonRightIsPressed == true)
{
currentSteerAngle = steerAngle * rb.transform.right.x;
frontRightWheelCollider.steerAngle = currentSteerAngle;
frontLeftWheelCollider.steerAngle = currentSteerAngle;
}
else
{
return;
}
if (ButtonLeftIsPressed == true)
{
currentSteerAngle = steerAngle * rb.transform.right.x * -1;
frontRightWheelCollider.steerAngle = currentSteerAngle;
frontLeftWheelCollider.steerAngle = currentSteerAngle;
}
else
{
return;
}
}
private void EngineHandler()
{
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
{
if (!engineAudioSourse.isPlaying)
{
engineAudioSourse.Play();
}
}
else
{
engineAudioSourse.Stop();
}
}
#region UIButtons
public void OnForwardButtonDown()
{
ButtonForwardIsPressed = true;
}
public void OnBackwardButtonDown()
{
ButtonBackwardIsPressed = true;
}
public void OnRightButtonDown()
{
ButtonRightIsPressed = true;
}
public void OnLeftButtonDown()
{
ButtonLeftIsPressed = true;
}
public void OnForwardButtonUp()
{
ButtonForwardIsPressed = false;
}
public void OnBackwardButtonUp()
{
ButtonBackwardIsPressed = false;
}
public void OnRightButtonUp()
{
ButtonRightIsPressed = false;
}
public void OnLeftButtonUp()
{
ButtonLeftIsPressed = false;
}
#endregion
}
private CharacterController character;
public float speed;
public float jumpHeight;
Vector3 velocity;
private float gravity = -10f;
public Transform groundCheck;
public LayerMask isGround;
public float Radius;
public bool isGrounded;
// Start is called before the first frame update
void Start()
{
character = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
//character move
Vector3 move = transform.right * x + transform.forward * z;
character.Move(move * speed * Time.deltaTime);
//character jump
velocity.y += gravity * Time.deltaTime;
character.Move(velocity * Time.deltaTime);
isGrounded = Physics.CheckSphere(groundCheck.position, Radius, isGround);
if (isGrounded == true && velocity.y < 0)
{
velocity.y = -2f;
}
if (isGrounded == true && Input.GetButtonDown("Jump"))
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
из метода Awake в конструктор класса.