fixed merge
This commit is contained in:
59
Assets/Scripts/BugMovement.cs
Normal file
59
Assets/Scripts/BugMovement.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class BugMovement : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float secondsGoingLeft = 1;
|
||||
[SerializeField] private float secondsGoingRight = 1;
|
||||
[SerializeField] private float movementSpeed = 10;
|
||||
[SerializeField] PolygonCollider2D playerCollider;
|
||||
|
||||
private int leftAccumalator = 0;
|
||||
private int rightAccumalator = 0;
|
||||
private Rigidbody2D rigidBody;
|
||||
|
||||
private int framesPerSecond => 60;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Physics2D.IgnoreCollision(GetComponent<BoxCollider2D>(), playerCollider);
|
||||
rigidBody = GetComponent<Rigidbody2D>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
var totalFramesGoingLeft = framesPerSecond * secondsGoingLeft;
|
||||
var totalFramesGoingRight = framesPerSecond * secondsGoingRight;
|
||||
|
||||
if (leftAccumalator <= totalFramesGoingLeft)
|
||||
{
|
||||
MoveLeft();
|
||||
leftAccumalator++;
|
||||
|
||||
if (leftAccumalator == totalFramesGoingLeft)
|
||||
rightAccumalator = 0;
|
||||
}
|
||||
else if (rightAccumalator <= totalFramesGoingRight)
|
||||
{
|
||||
MoveRight();
|
||||
rightAccumalator++;
|
||||
|
||||
if (rightAccumalator == totalFramesGoingRight)
|
||||
leftAccumalator = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void MoveLeft()
|
||||
{
|
||||
var actualSpeed = movementSpeed * -1;
|
||||
rigidBody.velocity = new Vector2(actualSpeed, rigidBody.velocity.y);
|
||||
}
|
||||
|
||||
private void MoveRight()
|
||||
{
|
||||
var actualSpeed = movementSpeed;
|
||||
rigidBody.velocity = new Vector2(actualSpeed, rigidBody.velocity.y);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/BugMovement.cs.meta
Normal file
11
Assets/Scripts/BugMovement.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e5f777aa09d2ced42baa1cc6bf9dbe54
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
22
Assets/Scripts/EventControlsDisplay.cs
Normal file
22
Assets/Scripts/EventControlsDisplay.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
public class EventControlsDisplay : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Camera camera;
|
||||
|
||||
private Tilemap tilemap;
|
||||
[SerializeField] private Tile tile;
|
||||
|
||||
void Start()
|
||||
{
|
||||
tilemap = GetComponent<Tilemap>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
tilemap.SetTile(new Vector3Int((int)camera.transform.position.x, (int)camera.transform.position.y, (int)camera.transform.position.z), tile);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/EventControlsDisplay.cs.meta
Normal file
11
Assets/Scripts/EventControlsDisplay.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 555a3f8aa89a0e84fb6c43249a117f93
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -7,18 +7,22 @@ public class Player : MonoBehaviour
|
||||
[SerializeField] private float runSpeed = 10f;
|
||||
[SerializeField] private float jumpSpeed = 10f;
|
||||
[SerializeField] private float runFasterFactor = 1.5f;
|
||||
[SerializeField] private float groundErrorThreshold = 0.01f;
|
||||
|
||||
private Rigidbody2D rigidbody;
|
||||
private PolygonCollider2D collider;
|
||||
private bool isInEncounter = false;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
rigidbody = GetComponent<Rigidbody2D>();
|
||||
collider = GetComponent<PolygonCollider2D>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
Debug.Log(HasEncounteredEnemy());
|
||||
//isInEncounter = HasEncounteredEnemy();
|
||||
//if (isInEncounter)
|
||||
MovePlayer();
|
||||
}
|
||||
|
||||
@@ -30,10 +34,8 @@ public class Player : MonoBehaviour
|
||||
|
||||
private void Jump()
|
||||
{
|
||||
var playerOnGround = rigidbody.IsTouchingLayers(LayerMask.GetMask(LayerNames.Ground));
|
||||
|
||||
// Jump
|
||||
if (Input.GetKey(KeyCode.Space) && playerOnGround)
|
||||
if (Input.GetKey(KeyCode.Space) && IsPlayerOnGround())
|
||||
{
|
||||
rigidbody.velocity += new Vector2(0, jumpSpeed);
|
||||
rigidbody.velocity = new Vector2(rigidbody.velocity.x,
|
||||
@@ -62,6 +64,16 @@ public class Player : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsPlayerOnGround()
|
||||
{
|
||||
RaycastHit2D hit = Physics2D.Raycast(transform.position,
|
||||
-Vector2.up,
|
||||
1f + groundErrorThreshold,
|
||||
LayerMask.GetMask(LayerNames.Ground));
|
||||
|
||||
return hit.collider != null && hit.collider.IsTouching(collider);
|
||||
}
|
||||
|
||||
private bool HasEncounteredEnemy()
|
||||
{
|
||||
return rigidbody.IsTouchingLayers(LayerMask.GetMask(LayerNames.Enemies));
|
||||
|
||||
Reference in New Issue
Block a user