[go: up one dir, main page]

0% found this document useful (0 votes)
27 views7 pages

All (1) .CPP 300

The document contains Unity C# scripts for a game, including classes for enemy movement, projectile behavior, aim assist, recoil control, and sensitivity adjustment. Key features include patrol points for enemies, damage handling for projectiles, and predictive aiming for targeting. Additionally, it incorporates camera recoil effects and sensitivity adjustments based on system RAM.

Uploaded by

funnyinfivefive
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views7 pages

All (1) .CPP 300

The document contains Unity C# scripts for a game, including classes for enemy movement, projectile behavior, aim assist, recoil control, and sensitivity adjustment. Key features include patrol points for enemies, damage handling for projectiles, and predictive aiming for targeting. Additionally, it incorporates camera recoil effects and sensitivity adjustments based on system RAM.

Uploaded by

funnyinfivefive
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <bits/stdc++.

h>
using namespace std;

int main() {
// your code goes here
using UnityEngine;

[RequireComponent(typeof(CharacterController))]
public class EnemyMovement : MonoBehaviour
{
public float speed = 2.5f;
public Transform[] patrolPoints;
public float changePointRadius = 0.5f;
public Transform headTransform; // assign child 'Head' in inspector
public Vector3 velocity;

int idx = 0;

void Start()
{
if (patrolPoints == null || patrolPoints.Length == 0)
{
// idle wiggle if no patrol points assigned
patrolPoints = new Transform[1];
GameObject go = new GameObject("temp_point");
go.transform.position = transform.position;
patrolPoints[0] = go.transform;
}
}

void Update()
{
if (patrolPoints.Length == 0) return;

Vector3 target = patrolPoints[idx].position;


Vector3 dir = (target - transform.position);
dir.y = 0;
if (dir.magnitude < changePointRadius)
{
idx = (idx + 1) % patrolPoints.Length;
return;
}
Vector3 move = dir.normalized * speed * Time.deltaTime;
transform.position += move;

// simple facing direction


if (move.sqrMagnitude > 0.0001f)
{
transform.forward = Vector3.Slerp(transform.forward, move.normalized,
8f * Time.deltaTime);
}

// expose velocity for predictive aiming


velocity = (target - transform.position).normalized * speed;
}

// Optional: draw head gizmo


void OnDrawGizmosSelected()
{
if (headTransform)
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(headTransform.position, 0.1f);
}
}
}using UnityEngine;

public class HeadHitbox : MonoBehaviour


{
public int health = 100;
public bool isAlive = true;

// Recommend this collider to be Trigger


void OnTriggerEnter(Collider other)
{
// Assume projectiles have tag "Projectile"
if (!isAlive) return;

if (other.CompareTag("Projectile"))
{
// Example damage pickup from projectile (projectile should carry
damage via script)
var proj = other.GetComponent<Projectile>();
int dmg = proj ? proj.damage : 25;
TakeDamage(dmg);
Destroy(other.gameObject);
}
}

public void TakeDamage(int amount)


{
health -= amount;
if (health <= 0)
{
isAlive = false;
Die();
}
}

void Die()
{
// simple disable, replace with ragdoll, animation, etc.
var root = transform.root.gameObject;
root.SetActive(false);
}
}
}using UnityEngine;

public class Projectile : MonoBehaviour


{
public int damage = 25;
public float speed = 50f;
public Vector3 direction;
public float life = 3f;

void Update()
{
transform.position += direction * speed * Time.deltaTime;
life -= Time.deltaTime;
if (life <= 0) Destroy(gameObject);
}using UnityEngine;

public class AimAssist : MonoBehaviour


{
public Camera playerCamera; // assign your camera
public float assistStrength = 1.0f; // 0 = none, 1 = full snapping (but we
smooth)
public float smoothTime = 0.08f; // how quickly camera rotates to target
public float projectileSpeed = 80f; // used for predictive aim
public LayerMask enemyLayer;
public LayerMask headLayer;
public float maxAssistDistance = 80f;
public KeyCode assistKey = KeyCode.Mouse1; // editor: hold right mouse

Vector3 currentAngularVelocity;

void Update()
{
bool assistHeld = Input.GetKey(assistKey) || IsMobileAimHeld();
if (!assistHeld) return;

// find closest visible head in view cone


Transform head = FindBestHeadTarget();
if (head == null) return;

// Predictive lead: estimate where head will be when projectile arrives


Vector3 aimPoint = PredictLeadPoint(playerCamera.transform.position,
projectileSpeed, head);

// Smooth rotate camera toward aimPoint


RotateCameraTowards(aimPoint);
}

bool IsMobileAimHeld()
{
// Simple mobile mapping: second touch on screen -> aim
if (Input.touchCount > 0)
{
foreach (Touch t in Input.touches)
{
if (t.phase == TouchPhase.Began || t.phase == TouchPhase.Stationary
|| t.phase == TouchPhase.Moved)
{
// treat any touch on right half-of-screen as aim
if (t.position.x > Screen.width * 0.5f) return true;
}
}
}
return false;
}

Transform FindBestHeadTarget()
{
Collider[] hits = Physics.OverlapSphere(playerCamera.transform.position,
maxAssistDistance, headLayer);
Transform best = null;
float bestScore = float.MaxValue;
foreach (var col in hits)
{
Transform head = col.transform;
Vector3 toHead = head.position - playerCamera.transform.position;
float angle = Vector3.Angle(playerCamera.transform.forward, toHead);
if (angle > 40f) continue; // ignore wide angles

// Check line of sight


RaycastHit rh;
if (Physics.Raycast(playerCamera.transform.position, toHead.normalized,
out rh, maxAssistDistance))
{
if (rh.collider != col) continue; // occluded
}

float dist = toHead.sqrMagnitude;


if (dist < bestScore)
{
bestScore = dist;
best = head;
}
}

return best;
}

Vector3 PredictLeadPoint(Vector3 shooterPos, float projSpeed, Transform


targetHead)
{
// Basic target velocity guess: try to get EnemyMovement velocity if
present
Vector3 targetPos = targetHead.position;
Rigidbody rb = targetHead.GetComponentInParent<Rigidbody>();
Vector3 v = Vector3.zero;
// fallback: try to find EnemyMovement on root
var em = targetHead.root.GetComponent<EnemyMovement>();
if (em != null)
v = em.velocity;
else if (rb != null)
v = rb.velocity;

Vector3 displacement = targetPos - shooterPos;


float a = Vector3.Dot(v, v) - projSpeed * projSpeed;
float b = 2f * Vector3.Dot(v, displacement);
float c = Vector3.Dot(displacement, displacement);

float t;
float disc = b * b - 4f * a * c;
if (Mathf.Abs(a) < 0.001f || disc < 0f)
{
// cannot solve quadratic -> fall back to aim at current pos
t = displacement.magnitude / projSpeed;
}
else
{
float sqrtD = Mathf.Sqrt(disc);
float t1 = (-b + sqrtD) / (2f * a);
float t2 = (-b - sqrtD) / (2f * a);
t = Mathf.Min(t1 > 0 ? t1 : float.MaxValue, t2 > 0 ? t2 :
float.MaxValue);
if (float.IsInfinity(t) || t <= 0) t = displacement.magnitude /
projSpeed;
}

Vector3 predicted = targetPos + v * t;


return predicted;
}

void RotateCameraTowards(Vector3 worldPoint)


{
Vector3 dir = worldPoint - playerCamera.transform.position;
Quaternion targetRot = Quaternion.LookRotation(dir.normalized, Vector3.up);

// Blend between current and target rotation using smooth damping


float t = Mathf.Clamp01(assistStrength);
Quaternion smoothed = Quaternion.Slerp(playerCamera.transform.rotation,
targetRot, 1f - Mathf.Exp(-smoothTime * 60f) * t);
// apply only yaw and pitch while preserving roll
playerCamera.transform.rotation = smoothed;
}
}using UnityEngine;

public class RecoilController : MonoBehaviour


{
public Camera cam;
public Vector2 recoilPerShot = new Vector2(0.6f, 0.2f); // x: vertical, y:
horizontal
public float recoilRecoverySpeed = 6f;
public float recoilRandomness = 0.4f; // spread variation
Vector2 recoilOffset = Vector2.zero;
Vector2 recoilVelocity = Vector2.zero;

void Start()
{
if (cam == null) cam = Camera.main;
}

void Update()
{
// Smoothly decay recoil over time (recovery)
recoilOffset = Vector2.SmoothDamp(recoilOffset, Vector2.zero, ref
recoilVelocity, 1f / recoilRecoverySpeed);
ApplyRecoilToCamera();
}

public void FireShot()


{
// apply vertical and horizontal recoil + random spread
float sign = Random.value > 0.5f ? 1f : -1f;
Vector2 random = new Vector2(Random.Range(0f, recoilRandomness),
Random.Range(-recoilRandomness, recoilRandomness));
recoilOffset += new Vector2(recoilPerShot.x, recoilPerShot.y * sign) +
random;
}

void ApplyRecoilToCamera()
{
// rotate camera locally by recoilOffset degrees (pitch, yaw)
Vector3 e = cam.transform.eulerAngles;
float pitch = e.x;
float yaw = e.y;

// convert eulerAngles.x from 0..360 to -180..180


pitch = NormalizeAngle(pitch);
yaw = NormalizeAngle(yaw);

float newPitch = pitch - recoilOffset.x; // negative to move view up


float newYaw = yaw + recoilOffset.y;

cam.transform.rotation = Quaternion.Euler(newPitch, newYaw, 0f);


}

float NormalizeAngle(float a)
{
while (a > 180f) a -= 360f;
while (a < -180f) a += 360f;
return a;
}
}using UnityEngine;

public class SensitivityController : MonoBehaviour


{
[Range(0.1f, 10f)]
public float baseSensitivity = 2.5f; // baseline sensitivity for 8GB RAM device

private float currentSensitivity;

void Start()
{
currentSensitivity = baseSensitivity;

// Optional: detect RAM size (simple)


int ramMB = SystemInfo.systemMemorySize; // in MB

if (ramMB >= 8000 && ramMB < 10000)


{
currentSensitivity = baseSensitivity; // optimized for 8GB device
}
else if (ramMB < 4000)
{
currentSensitivity = baseSensitivity * 0.8f; // lower sensitivity for
low RAM
}
else if (ramMB > 10000)
{
currentSensitivity = baseSensitivity * 1.2f; // slightly higher for
high-end devices
}
}

public float GetSensitivity()


{
return currentSensitivity;
}

// Example applying sensitivity to camera rotation on touch input:


public void UpdateCameraRotation(ref Vector2 rotationInput)
{
rotationInput *= currentSensitivity;
}
}
}

You might also like