In my mobile game I have multiple walls that the player should not be able to walk through. I thought I could simply add box colliders and a rigidbody on my walls and player like you normally do. But it doesn’t work. I browsed online and supposedly something should be written down in the player movement script to make this work?
I am very new to scripting so I have no idea what to do here. is there a script I could copy so the player is not going through the walls anymore? This is my player movement script at the moment:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstPersonController : MonoBehaviour
{
// Declaring the controls of player and camera:
[SerializeField] private Transform cameraTransform;
[SerializeField] private CharacterController characterController;
// Declaring the camera sensitivity, movespeed and, deadzones
[SerializeField] private float cameraSensitivity;
[SerializeField] private float moveSpeed;
[SerializeField] private float moveInputDeadZone;
private int leftFingerId, rightFingerId; // touch detection integer for left and right part of the screen
private float halfScreenWidth; //making the screen be split in half
// Camera controls
private Vector2 lookInput;
private float cameraPitch;
private Vector2 moveTouchStartPosition; // find the start position of the player
private Vector2 moveInput; //moving the player
void Start()
{
// both fingers are not touching the screen
leftFingerId = -1;
rightFingerId = -1;
// split the screen in 2
halfScreenWidth = Screen.width / 2;
// calculate the movement dead zone
moveInputDeadZone = Mathf.Pow(Screen.height / moveInputDeadZone, 2);
}
void Update()
{
// Handles input of touching
GetTouchInput();
if (rightFingerId != -1) //if the finger is touching the screen on the right side then...
{
Debug.Log("Rotating"); // info in the log
LookAround(); // look around
}
if (leftFingerId != -1) // if the finger is touching the screen on the left side then...
{
Debug.Log("Moving"); // info in the log
Move(); // then move the player
}
}
void GetTouchInput() {
// Iterate through all the detected touches
for (int i = 0; i < Input.touchCount; i++)
{
Touch t = Input.GetTouch(i);
// Check each touch's phase
switch (t.phase)
{
case TouchPhase.Began:
if (t.position.x < halfScreenWidth && leftFingerId == -1)
{
// Start tracking the left finger if it was not previously being tracked
leftFingerId = t.fingerId;
// Set the start position for the movement control finger
moveTouchStartPosition = t.position;
}
else if (t.position.x > halfScreenWidth && rightFingerId == -1)
{
// Start tracking the rightfinger if it was not previously being tracked
rightFingerId = t.fingerId;
}
break;
case TouchPhase.Ended:
case TouchPhase.Canceled:
if (t.fingerId == leftFingerId)
{
// Stop tracking the left finger
leftFingerId = -1;
Debug.Log("Stopped tracking left finger");
}
else if (t.fingerId == rightFingerId)
{
// Stop tracking the right finger
rightFingerId = -1;
Debug.Log("Stopped tracking right finger");
}
break;
case TouchPhase.Moved:
// Get input for looking around
if (t.fingerId == rightFingerId)
{
lookInput = t.deltaPosition * cameraSensitivity * Time.deltaTime;
}
else if (t.fingerId == leftFingerId) {
// calculating the position delta from the start position
moveInput = t.position - moveTouchStartPosition;
}
break;
case TouchPhase.Stationary:
// Set the look input to zero if the finger is still
if (t.fingerId == rightFingerId)
{
lookInput = Vector2.zero;
}
break;
}
}
}
void LookAround() {
// vertical (pitch) rotation
cameraPitch = Mathf.Clamp(cameraPitch - lookInput.y, -90f, 90f);
cameraTransform.localRotation = Quaternion.Euler(cameraPitch, 0, 0);
// horizontal (yaw) rotation
transform.Rotate(transform.up, lookInput.x);
}
void Move() {
// Don't move if the touch delta is shorter than the designated dead zone
if (moveInput.sqrMagnitude <= moveInputDeadZone) return;
// Multiply the normalized direction by the speed
Vector2 movementDirection = moveInput.normalized * moveSpeed * Time.deltaTime;
// Move relatively to the local transform's direction
characterController.Move(transform.right * movementDirection.x + transform.forward * movementDirection.y);
//linkToGuide.GuideTellswalkingMoving(); // werkt niet. :(
}
}