Ground check, separate camera pos for diff directions

This commit is contained in:
Norbi Peti 2020-11-06 01:56:50 +01:00
parent ca6add1438
commit b7a5715e34
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
7 changed files with 74 additions and 125 deletions

View file

@ -56,7 +56,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Assets\CameraController.cs" />
<Compile Include="Assets\CharacterController.cs" />
<Compile Include="Assets\OwnCharacterController.cs" />
<Compile Include="Assets\Tiling.cs" />
<Reference Include="UnityEngine">
<HintPath>/D/Unity/2019.4.13f1/Editor/Data/Managed/UnityEngine/UnityEngine.dll</HintPath>

View file

@ -1,38 +1,54 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Class for following the player with the camera
public class CameraController : MonoBehaviour {
// Camera speed
public float speed = 0.05f;
public float speed = 0.1f;
// GameObject to be followed.
public Transform target;
// Our own transform
private Transform tr;
private Transform _tr;
// Center position of the target relative to the camera.
public Vector3 offset;
private float _lastSwitch = -1;
private bool _facingRight;
// Store initial values
void Start()
{
tr = transform;
offset = target.position - tr.position;
_tr = transform;
offset = target.position - _tr.position;
}
// Update positions
void FixedUpdate ()
void FixedUpdate()
{
if (!target) return;
// Get where the camera should be, and what movement is required.
var position = tr.position;
Vector3 anchorPos = position + offset;
var position = _tr.position;
bool facingLeft = target.localScale.x < 0;
Vector3 anchorPos = position + (facingLeft ? new Vector3(-offset.x, offset.y, offset.z) : offset);
Vector3 movement = target.position - anchorPos;
float sp = speed;
/*if (_lastSwitch >= 0 && Time.time - _lastSwitch < 0.5f)
sp /= 4;*/
float diff = Time.time - _lastSwitch;
if (_lastSwitch >= 0 && diff < 2f)
sp *= 0.5f + diff / 4;
if (facingLeft == _facingRight) //Megváltozott az irány
_lastSwitch = Time.time;
_facingRight = !facingLeft;
// Update position based on movement and speed.
Vector3 newCamPos = position + movement*speed;
Vector3 newCamPos = position + movement * sp;
position = newCamPos;
tr.position = position;
_tr.position = position;
}
}

View file

@ -1,22 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterController : MonoBehaviour
{
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Mathf.Abs(rb.velocity.x) > 10)
return;
rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * 5, 0));
}
}

View file

@ -0,0 +1,47 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class OwnCharacterController : MonoBehaviour
{
private Rigidbody2D rb;
private Collider2D collider;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
collider = GetComponent<Collider2D>();
}
// Update is called once per frame
void Update()
{
if (Mathf.Abs(rb.velocity.x) > 3)
return;
float input = Input.GetAxis("Horizontal");
if (input < 0 && rb.transform.localScale.x > 0
|| input > 0 && rb.transform.localScale.x < 0)
{
var tr = transform;
var scale = tr.localScale;
scale.x *= -1;
tr.localScale = scale;
}
rb.AddForce(new Vector2(input * 5, 0));
var cols = new Collider2D[5];
/*if (Input.GetButtonDown("Jump") && rb.OverlapCollider(new ContactFilter2D(), cols) > 0
&& cols.Any(col => col.CompareTag("Tiled")))*/
if (Input.GetButtonDown("Jump") && IsOnGround())
rb.AddForce(new Vector2(0, 3), ForceMode2D.Impulse);
}
private bool IsOnGround()
{
var bounds = collider.bounds;
return Physics.CheckCapsule(bounds.center, new Vector3(bounds.center.x, bounds.min.y - 0.1f, bounds.center.z),
0.28f);
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

View file

@ -1,92 +0,0 @@
fileFormatVersion: 2
guid: 5fa1bba7bbdf8890ab8dcb6ee0f1cef2
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 11
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -100
wrapU: 1
wrapV: 1
wrapW: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 8
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
applyGammaDecoding: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID: 5e97eb03825dee720800000000000000
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant: