Hello I have a problem in here can someone help me address it. When the Player made contact with NipaHutTrigger cube with a trigger already checked in the inspector panel, then the MainCameraController object named gap will change from 9 to 3 and when made contact again, then it will change the gap from 3 to 9. Is there a way to address this problem?
These are my Scripts
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainCameraController : MonoBehaviour
{
[Header("Camera Controller")]
public Transform target;
public float gap = 9f;
public float rotSpeed = 3f;
[Header("Camera Handling")]
public float minVerAngle = -14f;
public float maxVerAngle = 45f;
public Vector2 framingbalance;
float rotX;
float rotY;
public bool InvertX;
public bool InvertY;
float invertXValue;
float invertYValue;
public void Start ()
{
Cursor.lockState = CursorLockMode.Locked;
}
using UnityEngine;
public class NipaHutTrigger : MonoBehaviour
{
private bool isGap3 = false; // Track whether the gap is currently 3 or 9
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
MainCameraController cameraController = FindObjectOfType<MainCameraController>();
if (cameraController != null)
{
// Toggle the gap value
if (isGap3)
{
cameraController.gap = 9f;
isGap3 = false;
}
else
{
cameraController.gap = 3f;
isGap3 = true;
}
}
else
{
Debug.LogError("MainCameraController not found in the scene!");
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScript : MonoBehaviour
{
[Header("Player Movement")]
public float movementSpeed = 5f;
public float rotSpeed = 600f;
public MainCameraController MCC;
Quaternion requiredRotation;
[Header("Player Animator")]
public Animator animator;
[Header("Player Collision and Gravity")]
public CharacterController CC;
public float surfaceCheckRadius = 0.1f;
public Vector3 surfaceCheckOffset;
public LayerMask surfaceLayer;
bool onSurface;
[SerializeField] float fallingSpeed;
[SerializeField] Vector3 moveDir;
private void Update()
{
SurfaceCheck(); // Check if the player is on the surface
PlayerMovement(); // Handle player movement input and rotation
ApplyGravity(); // Apply gravity to the player
MovePlayer(); // Move the player based on calculated velocity
Debug.Log("Player on Surface: " + onSurface); // Added colon for clarity in the log
}
void PlayerMovement()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
float movementAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
Vector3 movementInput = new Vector3(horizontal, 0, vertical).normalized;
// Set moveDir based on input and camera's flat rotation
moveDir = MCC.flatRotation * movementInput;
if (movementAmount > 0)
{
// Update requiredRotation to face the movement direction
requiredRotation = Quaternion.LookRotation(moveDir);
}
// Smoothly rotate the player towards the required rotation
transform.rotation = Quaternion.RotateTowards(transform.rotation, requiredRotation, rotSpeed * Time.deltaTime);
animator.SetFloat("movementValue", movementAmount, 0.2f, Time.deltaTime);
}
// New method to handle gravity application
void ApplyGravity()
{
if (onSurface && fallingSpeed < 0)
{
// Use a small negative value to keep the player grounded
fallingSpeed = -0.5f;
}
else
{
// Apply gravity over time
fallingSpeed += Physics.gravity.y * Time.deltaTime;
}
}
// New method to move the player
void MovePlayer()
{
// Combine horizontal movement with vertical falling speed
Vector3 velocity = moveDir * movementSpeed;
velocity.y = fallingSpeed;
// Apply the calculated velocity to the CharacterController
CC.Move(velocity * Time.deltaTime);
}
void SurfaceCheck()
{
// Check if the player is on the surface
onSurface = Physics.CheckSphere(transform.TransformPoint(surfaceCheckOffset), surfaceCheckRadius, surfaceLayer);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(transform.TransformPoint(surfaceCheckOffset), surfaceCheckRadius);
}
}