So I tried to make the same thing Rick made. But, in a different way.
But when my player ship moves up to the clamped position, it moves the ship forward.
Does anyone have an explaination for this behavior? THX!
[Footage of my dumbdumb bug:]
(https://youtu.be/wnNKLVsQex0)
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] float playerspeed = 10f;
[SerializeField] float positionPitchFactor = -2f;
void Update()
{
MovePlayer();
}
void MovePlayer()
{
PlayerInputMove();
ClampPlayerShipInScreen();
ProcessShipRotation();
}
void ProcessShipRotation()
{
float pitch = transform.localPosition.y * positionPitchFactor;
float yaw = transform.localPosition.x * positionPitchFactor;
float roll = 0f;
transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
}
void PlayerInputMove()
{
float XThrow = Input.GetAxis("Horizontal") * playerspeed * Time.deltaTime;
float yThrow = Input.GetAxis("Vertical") * playerspeed * Time.deltaTime;
transform.Translate(XThrow, yThrow, 0f);
}
void ClampPlayerShipInScreen()
{
Vector3 clampedPos = transform.localPosition;
clampedPos.x = Mathf.Clamp(clampedPos.x, -5f, 5f);
clampedPos.y = Mathf.Clamp(clampedPos.y, -2f, 8f);
transform.localPosition = clampedPos;
}
}