Using unityengine;
Using unityengine. Inputsystem;
Public class playercontroller: Monobehaviour.
{
Charactercontroller Controller;
Playercontrols playercontrols;
Camera playeracağımera;
[Header("Movement")]
Public float walkspeed = 5F, sprintspeed = 8F, crouchspeed = 2.5F;
Public float jumpheight = 1.5F, gravity = -9.81F;
Vector3 velocity;
Bool isgrounded, iscrouching;
Float origh;
Vector3 origc;
Public float crouchh = 0.9F;
[Header("Aiming")]
Public float deffov = 60F, aimfov = 30F, aimspeed = 10F;
Bool isaiming;
[Header("Shooting")]
Public transform firepoint;
Public layermask shootmask;
Public float firerate = 0.1F, shootdist = 100F;
Float nextfire;
[Header("Weapons")]
Public gameobject[] weapons;
int wIdx;
[Header("Abilities")]
Public float cdbasic = 5F, cdsig = 10F, cdtok = 15F, cdult = 60F;
Float lastbasic = -mathf. Infinity, lastsig = -mathf. Infinity;
Float lasttok = -mathf. Infinity, lastult = -mathf. Infinity;
Void Awake()
{
Controller = GetComponent<CharacterController>();
Playeracağımera = camera. Main;
Origh = Controller. Height;
Origc = Controller. Center;
Playercontrols = New PlayerControls();
InitializeWeapons();
Var p = playercontrols. Player;
P. Jump. Performed += _ => Jump();
P. Crouch. Performed += _ => Crouch();
P. Crouch. Canceled += _ => StandUp();
P. Aim. Performed += _ => isaiming = true;
P. Aim. Canceled += _ => isaiming = false;
P. Changeweapon. Performed += ctx => ChangeWeapon(ctx.ReadValue<Vector2>());
P. Ability_basic. Performed += _ => UseBasic();
P. Ability_signature. Performed += _ => UseSig();
P. Ability_token. Performed += _ => UseTok();
P. Ability_ultimate. Performed += _ => UseUlt();
}
Void OnEnable()
{
playerControls.Player.Enable();
}
Void OnDisable()
{
playerControls.Player.Disable();
Var p = playercontrols. Player;
P. Jump. Performed -= _ => Jump();
P. Crouch. Performed -= _ => Crouch();
P. Crouch. Canceled -= _ => StandUp();
P. Aim. Performed -= _ => isaiming = true;
P. Aim. Canceled -= _ => isaiming = false;
P. Changeweapon. Performed -= ctx => ChangeWeapon(ctx.ReadValue<Vector2>());
P. Ability_basic. Performed -= _ => UseBasic();
P. Ability_signature. Performed -= _ => UseSig();
P. Ability_token. Performed -= _ => UseTok();
P. Ability_ultimate. Performed -= _ => UseUlt();
}
Void Update()
{
İsgrounded = Controller. İsgrounded;
İf (isgrounded && velocity. Y < 0) velocity. Y = -2F;
Vector2 mv = playerControls.Player.Move.ReadValue<Vector2>();
Vector3'dir = transform. Right * mv. X + transform. Forward * mv. Y;
Float SPD = walkspeed;
İf (playerControls.Player.Sprint.IsPressed())
SPD = iscrouching? Crouchspeed: Sprintspeed;
Else if (iscrouching) SPD = crouchspeed;
Controller. Move(dir * SPD * time. Deltatime);
Velocity. Y += gravity * time. Deltatime;
Controller. Move(velocity * time. Deltatime);
İf (playeracağımera)
{
Float tgt = isaiming? Aimfov: Deffov;
Playeracağımera. Fieldofview = mathf. Lerp(playeracağımera. Fieldofview, tgt, aimspeed * time. Deltatime);
}
İf (playerControls.Player.Fire.IsPressed() && Time.time >= nextFire)
{
Shoot();
Nextfire = time. Time + 1F / firerate;
}
}
Void Jump()
{
İf (isgrounded) velocity. Y = mathf. Sqrt(jumpheight * -2F * gravity);
Debug.Log("zıpladı!");
}
Void Crouch()
{
İf (!iscrouching)
{
İscrouching = true;
Controller. Height = crouchh;
Controller. Center = New Vector3(origc. X, crouchh / 2F, origc. Z);
Debug.Log("eğildi!");
}
Else.
{
StandUp();
}
}
Void StandUp()
{
İf (!iscrouching) return;
Vector3 rayorigin = transform. Position + Controller. Center;
Float raydistance = origh - crouchh + 0.1F;
İf (physics. Raycast(rayorigin, Vector3.up, raydistance, shootmask))
{
Debug.Log("üstte engel var, kalkılamıyor.");
Return;
}
İscrouching = false;
Controller. Height = origh;
Controller. Center = origc;
Debug.Log("ayağa kalktı!");
}
Void Shoot()
{
İf (firepoint == null)
{
Debug.Logwarning("firepoint (ateş noktası) belirlenmemiş! Inspector'da atayın.");
Return;
}
Raycasthit h;
İf (physics. Raycast(playeracağımera. Transform. Position, playeracağımera. Transform. Forward,
Out H, shootdist, shootmask))
{
Debug.Log("vurulan: " + h. Transform. Name + " at " + h. Point);
}
Else.
{
Debug.Log("hiçbir yere isabet etmedi.");
}
}
Private Void InitializeWeapons()
{
İf (weapons == null || weapons. Length == 0)
{
Debug.Logwarning("weapon models dizisi boş veya null! Hiçbir silah modeli atanmamış. Inspector'dan atayın.");
Return;
}
For (int i = 0; i < weapons. Length; i++)
{
İf (weapons!= null)
weapons.setactive(false);
}
if (weapons[wIdx]!= null)
{
weapons[wIdx].setactive(true);
Debug.Log("başlangıç silahı: " + weapons[wIdx].name);
}
}
Void changeweapon(Vector2 s)
{
if (weapons == null || weapons. Length == 0) return;
if (wıdx >= 0 && wıdx < weapons. Length && weapons[wIdx]!= null)
{
weapons[wIdx].setactive(false);
}
if (s. Y > 0)
wıdx = (wıdx + 1) % weapons. Length;
else if (s. Y < 0)
wıdx = (wıdx - 1 + weapons. Length) % weapons. Length;
if (wıdx >= 0 && wıdx < weapons. Length && weapons[wIdx]!= null)
{
weapons[wIdx].setactive(true);
Debug.Log("silah değiştirildi: " + weapons[wIdx].name);
}
}
Void UseBasic()
{
if (time. Time - lastbasic >= cdbasic)
{
lastbasic = time. Time;
Debug.Log("temel yetenek kullanıldı!");
}
else { Debug.Log("temel yetenek beklemede. Kalan süre: " + (cdbasic - (time. Time - lastbasic)).tostring("F1") + "s"); }
}
Void UseSig()
{
if (time. Time - lastsig >= cdsig)
{
lastsig = time. Time;
Debug.Log("imza yetenek kullanıldı!");
}
else { Debug.Log("imza yetenek beklemede. Kalan süre: " + (cdsig - (time. Time - lastsig)).tostring("F1") + "s"); }
}
Void UseTok()
{
if (time. Time - lasttok >= cdtok)
{
lasttok = time. Time;
Debug.Log("jetonlu yetenek kullanıldı!");
}
else { Debug.Log("jetonlu yetenek beklemede. Kalan süre: " + (cdtok - (time. Time - lasttok)).tostring("F1") + "s"); }
}
Void UseUlt()
{
if (time. Time - lastult >= cdult)
{
lastult = time. Time;
Debug.Log("Ultimate yetenek kullanıldı!");
}
else { Debug.Log("Ultimate yetenek beklemede. Kalan süre: " + (cdult - (time. Time - lastult)).tostring("F1") + "s"); }
}
}