I am new to Unity, C# or to OOP in general.
I am learning Unity right now, and all of the Scripts that I use are self made. Ai helped a little bit to like understand how to even write C# and so on. Before I started I read the entire W3Schools Documentation about C#, this helped alot to understand how OOP works. The Problem with AI (Github Copilot) is that it doesnt works all the time (has old Information) and W3School doesnt explains Unity. The Documentation from Unity itself, is very rough and I understand nothing. I look 90% up on Yt it helped but its always stuck with only one of the different solutions that are possible.
My Movement Script:
using System;
using UnityEngine;
using UnityEngine.InputSystem;
public class Movement : MonoBehaviour
{
public LayerMask ground;
public float jumpforce = 8f;
public float accel = 8f;
public float friction = 2f;
public float maxSpeed;
public float rotationSpeed = 1f;
private bool isGroundTouched = false;
private Rigidbody2D rb;
private Collider2D col;
Vector2 movementDirection = Vector2.zero;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
col = GetComponent<Collider2D>();
}
private void Update()
{
isGroundTouched = col.IsTouchingLayers(ground);
if (Input.GetKeyDown(KeyCode.R))
{
rb.position = (new Vector2(0, 0));
}
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
private void FixedUpdate()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
movementDirection = new Vector2(horizontalInput, verticalInput).normalized;
if (movementDirection != Vector2.zero)
{
float angle = Mathf.Atan2(movementDirection.y, movementDirection.x) * Mathf.Rad2Deg;
rb.rotation = angle;
}
if (Input.GetKey(KeyCode.D))
{
horizontalInput = accel;
}
else if (Input.GetKey(KeyCode.A))
{
horizontalInput = -accel;
}
else
{
ApplyFriction();
}
if (!Mathf.Approximately(horizontalInput, 0f))
{
Move(horizontalInput);
}
LimitSpeed();
}
private void Move(float direction)
{
rb.AddForce(new Vector2(direction, 0));
}
private void ApplyFriction()
{
if (Mathf.Approximately(rb.linearVelocity.x, 0f))
{
rb.linearVelocity = new Vector2(0, rb.linearVelocity.y);
}
else
{
rb.linearVelocity = new Vector2(rb.linearVelocity.x * (1 - friction * Time.fixedDeltaTime), rb.linearVelocity.y);
}
}
private void LimitSpeed()
{
if (rb.linearVelocity.x > maxSpeed)
{
rb.linearVelocity = new Vector2(maxSpeed, rb.linearVelocity.y);
}
else if (rb.linearVelocity.x < -maxSpeed)
{
rb.linearVelocity = new Vector2(-maxSpeed, rb.linearVelocity.y);
}
}
private void Jump()
{
if (isGroundTouched)
{
rb.AddForce(new Vector2(rb.linearVelocity.x, jumpforce));
}
}
}
My Item Pickup:
using System;
using Unity.VisualScripting;
using UnityEngine;
public class ItemPickup : MonoBehaviour
{
public GameObject[] items;
public Transform grabpoint;
public Transform raypoint;
public float grabDistance;
public LayerMask ItemLayer;
private GameObject grabbedItem;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
RaycastHit2D hit = Physics2D.Raycast(raypoint.position, transform.right, grabDistance, ItemLayer);
if (hit.collider != null)
{
if (Input.GetKeyDown(KeyCode.Q) && grabbedItem == null)
{
grabbedItem = hit.collider.gameObject;
grabbedItem.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
grabbedItem.transform.position = grabpoint.position;
grabbedItem.transform.SetParent(transform);
}
else if (Input.GetKeyDown(KeyCode.Q))
{
grabbedItem.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
grabbedItem.transform.SetParent(null);
grabbedItem = null;
}
}
}
}
My Camara Follow:
using UnityEngine;
public class CamaraFollow : MonoBehaviour
{
public Transform target;
public float yOffset = 1.5f;
public float CamSpeed = 0.125f;
// Update is called once per frame
void Update()
{
Vector3 newpos = new Vector3(target.position.x, target.position.y + yOffset, -20f);
transform.position = Vector3.Slerp(transform.position, newpos, CamSpeed+Time.deltaTime);
}
}
Im looking forward to doing an Attck and Parry System, but dont know how to even start with it. I found a good looking script in the Unity Community forum from 2023.
Hope you can help me to get it working.
best regards
Fabian