NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CharacterController2D : MonoBehaviour {

private Vector2 _velocity;

private BoxCollider2D _boxCollider;//2
private Vector3 _localScale;//2

private Vector3 _raycastBottomRight;
private Vector3 _raycastBottomLeft;
private Vector3 _raycastTopLeft;
private Transform _transform;
private const float SkinWidth = 0.02f;
private const int TotalHorizontalrays = 8;
private const int TotalVerticalrays = 8;
private float _verticalDistanceBetweenRays;
private float _horizontalDistanceRays;
private Vector3 _activeGlobalPlatformPoint;
private Vector3 _activeLocalPlatformPoint;


public LayerMask PlatformMask;
public Vector2 Velocity { get { return _velocity; } }
public GameObject StandingOn { get; private set; }


public void LateUpdate()
{
_velocity.y += -25f * Time.deltaTime;
Move(Velocity*Time.deltaTime); //3 Hareketi sağlayacak olan fonksiyon tanımlıyoruz.
}

private void Move(Vector2 deltaMovement) //4. Kısa süreli hareket için bir vektör tanımlaması yapıldı
{
HandlePlatforms();
CalculateRayOrigins(); //Hareket etmeden önce boxcolliderin dış mesafelerini hesaplasın
MoveHorizontally(ref deltaMovement);
MoveVertically(ref deltaMovement);

_transform.Translate(deltaMovement, Space.World); //5.koordinat sistemini kullanarak hareketi sağlayacak. Sürekli hareketi sağlar.
if (Time.deltaTime > 0)
_velocity = deltaMovement / Time.deltaTime;
}


private void HandlePlatforms()
{
if (StandingOn!= null)
{
var newGlobalPlatformPoint = StandingOn.transform.TransformPoint(_activeLocalPlatformPoint);
var moveDistance = newGlobalPlatformPoint - _activeGlobalPlatformPoint;
if (moveDistance != Vector3.zero)
transform.Translate(moveDistance, Space.World);
}
// StandingOn = null;
}


public void Awake() //En Önce Sonra Update , Updateden sonra Late Update
{
_boxCollider = GetComponent<BoxCollider2D>();
_localScale = transform.localScale;
_transform = transform;

var ColliderHeight = _boxCollider.size.y * Mathf.Abs(transform.localScale.y) - (2 * SkinWidth);
var ColliderWidth= _boxCollider.size.x * Mathf.Abs(transform.localScale.x) - (2 * SkinWidth);
_horizontalDistanceRays=ColliderWidth / (TotalHorizontalrays - 1);
_verticalDistanceBetweenRays = ColliderHeight / (TotalVerticalrays - 1);
}

public void Jump()
{
AddForce(new Vector2(0, 16f));
}
public void AddForce(Vector2 force)
{
_velocity += force;
}





private void MoveHorizontally(ref Vector2 deltaMovement)
{
var isGoingRight = deltaMovement.x > 0;
var rayDistance = Mathf.Abs(deltaMovement.x) + SkinWidth;
var rayDirection = isGoingRight ? Vector2.right : -Vector2.right;
var rayOrigin = isGoingRight ? _raycastBottomRight : _raycastBottomLeft;

for (var i = 0; i < TotalHorizontalrays; i++)
{
var rayVector = new Vector2(rayOrigin.x, rayOrigin.y + (i * _verticalDistanceBetweenRays));
var raycastHit = Physics2D.Raycast(rayVector, rayDirection, rayDistance, PlatformMask);

if (!raycastHit)
continue;

deltaMovement.x = raycastHit.point.x - rayVector.x;
if (isGoingRight)
{
deltaMovement.x -= SkinWidth;
}
else { deltaMovement.x += SkinWidth; }

} //for
}
private void MoveVertically (ref Vector2 deltaMovement)
{
var isGoingUp = deltaMovement.y > 0;
var rayDistance = Mathf.Abs(deltaMovement.y) + SkinWidth;
var rayDirection = isGoingUp ? Vector2.up : -Vector2.up;
var rayOrigin = isGoingUp ? _raycastTopLeft : _raycastBottomLeft;
rayOrigin.x += deltaMovement.x;

for (var i=0;i<TotalVerticalrays;i++)
{
var rayVector = new Vector2(rayOrigin.x + (i * _horizontalDistanceRays), rayOrigin.y);
var raycastHit = Physics2D.Raycast(rayVector, rayDirection, rayDistance, PlatformMask);
if (!raycastHit)
continue;
//platform üstünde durmsı

if(!isGoingUp)
{
StandingOn = raycastHit.collider.gameObject;
Debug.Log(StandingOn.name);
}//
deltaMovement.y = raycastHit.point.y - rayVector.y;
if (isGoingUp)
{
deltaMovement.y -= SkinWidth;
}
else
{
deltaMovement.y += SkinWidth;
}
}
if (StandingOn!=null)
{
_activeGlobalPlatformPoint = transform.position;
_activeLocalPlatformPoint = StandingOn.transform.InverseTransformPoint(transform.position);
}
}

private void CalculateRayOrigins()
{
var size = new Vector2(_boxCollider.size.x * Mathf.Abs(_localScale.x), _boxCollider.size.y * Mathf.Abs(_localScale.y)) / 2;
var center = new Vector2(_boxCollider.offset.x * _localScale.x, _boxCollider.offset.y * _localScale.y);


_raycastBottomRight = _transform.position + new Vector3(center.x + size.x - SkinWidth, center.y - size.y + SkinWidth);
_raycastBottomLeft = _transform.position + new Vector3(center.x - size.x + SkinWidth, center.y - size.y + SkinWidth);
_raycastTopLeft = _transform.position + new Vector3(center.x - size.x + SkinWidth, center.y + size.y - SkinWidth);
}

public void SetHorizontalForce(float x)
{
_velocity.x = x;

}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.