И так, я создал скрипт для игрока, MineCamera, и для машины. Но вот проблема, когда я хожу, бегаю, прыгаю за игрока, моя "Статичная" как я думал машина едет точ в точ по моим движениям, что я сделал не так? Знающие люди которые уже сталкивались с такой проблемой объясните пожалуйста что не так?
Когда я еду на машине, игрок также передвигается точ в точ как машина.
Код на игрока
spoilerusing System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCntrl : MonoBehaviour
{
[Header("Скорость передвижения персонажа")]
public float speed = 7f;
[Header("Мы на земле")]
public bool ground;
public Rigidbody rb;
[Header("Сила прыжка")]
public float jumpPower = 200f;
private void Update() {
GetInput();
}
private void GetInput() {
if(Input.GetKey(KeyCode.W))
{
transform.localPosition += transform.forward * speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.S))
{
transform.localPosition += -transform.forward * speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.A))
{
transform.localPosition += -transform.right * speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.D))
{
transform.localPosition += transform.right * speed * Time.deltaTime;
}
if(Input.GetKeyDown(KeyCode.Space))
{
if(ground == true)
{
rb.AddForce(transform.up * jumpPower);
}
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "ground")
{
ground = true;
}
}
private void OnCollisionExit(Collision collision)
{
if(collision.gameObject.tag == "ground")
{
ground = false;
}
}
}
Код на камеру
spoilerusing System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraCntrl : MonoBehaviour
{
private float mouseX;
private float mouseY;
[Header("Скорость передвижения персонажа")]
public float sensitivityMouse = 200f;
public Transform Player;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
mouseX = Input.GetAxis("Mouse X") * sensitivityMouse * Time.deltaTime;
mouseY = Input.GetAxis("Mouse Y") * sensitivityMouse * Time.deltaTime;
Player.Rotate(mouseX * new Vector3(0, 1, 0));
transform.Rotate(-mouseY * new Vector3(1, 0, 0));
// Player.Rotate(-mouseY * new Vector3(0, 1, 0));
}
}
Код на машину
spoilerusing System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
private const string HORIZONTAL = "Horizontal";
private const string VERTICAL = "Vertical";
private float horizontalInput;
private float verticalInput;
private float currentSteerAngle;
private float currentbreakForce;
private bool isBreaking;
[SerializeField] private float motorForce;
[SerializeField] private float breakForce;
[SerializeField] private float maxSteerAngle;
[SerializeField] private WheelCollider frontLeftWheelCollider;
[SerializeField] private WheelCollider frontRightWheelCollider;
[SerializeField] private WheelCollider rearLeftWheelCollider;
[SerializeField] private WheelCollider rearRightWheelCollider;
[SerializeField] private Transform frontLeftWheelTransform;
[SerializeField] private Transform frontRightWheeTransform;
[SerializeField] private Transform rearLeftWheelTransform;
[SerializeField] private Transform rearRightWheelTransform;
private void FixedUpdate()
{
GetInput();
HandleMotor();
HandleSteering();
UpdateWheels();
}
private void GetInput()
{
horizontalInput = Input.GetAxis(HORIZONTAL);
verticalInput = Input.GetAxis(VERTICAL);
isBreaking = Input.GetKey(KeyCode.Space);
}
private void HandleMotor()
{
frontLeftWheelCollider.motorTorque = verticalInput * motorForce;
frontRightWheelCollider.motorTorque = verticalInput * motorForce;
currentbreakForce = isBreaking ? breakForce : 0f;
ApplyBreaking();
}
private void ApplyBreaking()
{
frontRightWheelCollider.brakeTorque = currentbreakForce;
frontLeftWheelCollider.brakeTorque = currentbreakForce;
rearLeftWheelCollider.brakeTorque = currentbreakForce;
rearRightWheelCollider.brakeTorque = currentbreakForce;
}
private void HandleSteering()
{
currentSteerAngle = maxSteerAngle * horizontalInput;
frontLeftWheelCollider.steerAngle = currentSteerAngle;
frontRightWheelCollider.steerAngle = currentSteerAngle;
}
private void UpdateWheels()
{
UpdateSingleWheel(frontLeftWheelCollider, frontLeftWheelTransform);
UpdateSingleWheel(frontRightWheelCollider, frontRightWheeTransform);
UpdateSingleWheel(rearRightWheelCollider, rearRightWheelTransform);
UpdateSingleWheel(rearLeftWheelCollider, rearLeftWheelTransform);
}
private void UpdateSingleWheel(WheelCollider wheelCollider, Transform wheelTransform)
{
Vector3 pos;
Quaternion rot
; wheelCollider.GetWorldPose(out pos, out rot);
wheelTransform.rotation = rot;
wheelTransform.position = pos;
}
}