Commit a6f26075 authored by mmaluschnig's avatar mmaluschnig
Browse files

created a character script that aligns the bottom of characters colliders with ground level.

parent a3317476
fileFormatVersion: 2
guid: 18ee7055881ef43f5a148f5569205745
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1001 &100100000
Prefab:
m_ObjectHideFlags: 1
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications: []
m_RemovedComponents: []
m_SourcePrefab: {fileID: 0}
m_RootGameObject: {fileID: 1993203501856666}
m_IsPrefabAsset: 1
--- !u!1 &1993203501856666
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
serializedVersion: 6
m_Component:
- component: {fileID: 4490612100453362}
- component: {fileID: 212154887090743764}
- component: {fileID: 61809397451393846}
- component: {fileID: 114803991258325674}
m_Layer: 0
m_Name: AhavaBaird
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &4490612100453362
Transform:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1993203501856666}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: -2.83289, y: 0.38500023, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!61 &61809397451393846
BoxCollider2D:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1993203501856666}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 1
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0.06, y: -0.34}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0.5, y: 0.5}
oldSize: {x: 2.56, y: 3.5}
newSize: {x: 2.56, y: 3.5}
adaptiveTilingThreshold: 0.5
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 3.19, y: 2.57}
m_EdgeRadius: 0
--- !u!114 &114803991258325674
MonoBehaviour:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1993203501856666}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9c41dca42caf14e17b64ff95fd032786, type: 3}
m_Name:
m_EditorClassIdentifier:
groundLevel: -4
--- !u!212 &212154887090743764
SpriteRenderer:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1993203501856666}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 4294967295
m_Materials:
- {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 0
m_SelectedEditorRenderState: 0
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_Sprite: {fileID: 21300000, guid: 15fe3851b7de048f4816224babe75f78, type: 3}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_Size: {x: 2.56, y: 3.5}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
fileFormatVersion: 2
guid: 49c8478baca8d47be993db0c6427da2e
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 100100000
userData:
assetBundleName:
assetBundleVariant:
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1001 &100100000
Prefab:
m_ObjectHideFlags: 1
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications: []
m_RemovedComponents: []
m_SourcePrefab: {fileID: 0}
m_RootGameObject: {fileID: 1015016174570030}
m_IsPrefabAsset: 1
--- !u!1 &1015016174570030
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
serializedVersion: 6
m_Component:
- component: {fileID: 4980212071913450}
- component: {fileID: 212980951982960026}
- component: {fileID: 50678151448700376}
- component: {fileID: 114510536981271840}
- component: {fileID: 70044034702610398}
m_Layer: 0
m_Name: Player
m_TagString: Player
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &4980212071913450
Transform:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1015016174570030}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 1.271, y: -2.69, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!50 &50678151448700376
Rigidbody2D:
serializedVersion: 4
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1015016174570030}
m_BodyType: 0
m_Simulated: 1
m_UseFullKinematicContacts: 0
m_UseAutoMass: 1
m_Mass: 2.5327892
m_LinearDrag: 0
m_AngularDrag: 0.05
m_GravityScale: 0
m_Material: {fileID: 0}
m_Interpolate: 0
m_SleepingMode: 1
m_CollisionDetection: 0
m_Constraints: 4
--- !u!70 &70044034702610398
CapsuleCollider2D:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1015016174570030}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 0
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0.12, y: -0.06}
m_Size: {x: 1.11, y: 2.52}
m_Direction: 0
--- !u!114 &114510536981271840
MonoBehaviour:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1015016174570030}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f353158edc01b4c5db5f065e4c8cb941, type: 3}
m_Name:
m_EditorClassIdentifier:
speed: 2
gravity: 9.8
jumpForce: 4
groundLevel: -2.7
--- !u!212 &212980951982960026
SpriteRenderer:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1015016174570030}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 4294967295
m_Materials:
- {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 0
m_SelectedEditorRenderState: 0
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_Sprite: {fileID: 21300000, guid: 3fa214d604776462cb99a2d4f78a8f79, type: 3}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_Size: {x: 2.56, y: 2.56}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
fileFormatVersion: 2
guid: 72a0d040ea21c4eebab30d68d3311671
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 100100000
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterMovement : MonoBehaviour {
private float groundLevel;
private float colliderBottom;
// Use this for initialization
void Start () {
groundLevel = DaytimeManager.groundLevel;
}
// Update is called once per frame
void Update()
{
//walk around randomly. if character enters their hitbox stop and face character.
findColliderBottom();
//if feet(bottom of collider) on ground or bellow
if (colliderBottom <= groundLevel)
{
//reset position to the ground;
Vector2 newPos = transform.position;
newPos.y += (groundLevel - colliderBottom);
transform.position = newPos;
}
}
void findColliderBottom(){
BoxCollider2D thisCollider = GetComponent<BoxCollider2D>();
float colliderPositionY = transform.position.y + thisCollider.offset.y;
colliderBottom = colliderPositionY - (thisCollider.size.y / 2);
}
}
fileFormatVersion: 2
guid: 9c41dca42caf14e17b64ff95fd032786
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DaytimeManager : MonoBehaviour {
public static float groundLevel = -4;
}
fileFormatVersion: 2
guid: 178d09aa2704a45b68d85008d69c7e95
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -7,16 +7,19 @@ public class PlayerMovement : MonoBehaviour {
public float speed = 2;
public float gravity = 9.8f;
public float jumpForce = 2;
public float groundLevel = -2.7f;
private Rigidbody2D body;
private float moveHorizontal;
private float vertSpeed;
private bool isGrounded;
private float groundLevel;
private bool facingRight;
// Use this for initialization
void Start () {
body = GetComponent<Rigidbody2D>();
groundLevel = DaytimeManager.groundLevel;
vertSpeed = 0;
facingRight = true;
}
......@@ -32,10 +35,14 @@ public class PlayerMovement : MonoBehaviour {
}
bool checkGrounded(){
if(transform.position.y <= groundLevel){
float colBottom = findColliderBottom();
//if feet(bottom of collider) on ground
if(colBottom <= groundLevel){
vertSpeed = 0;
Vector2 newPos = transform.position;
newPos.y = groundLevel;
newPos.y += (groundLevel - colBottom);
transform.position = newPos;
return true;
}else{
......@@ -72,4 +79,10 @@ public class PlayerMovement : MonoBehaviour {
newScale.x *= -1;
transform.localScale = newScale;
}
float findColliderBottom()
{
CapsuleCollider2D thisCollider = GetComponent<CapsuleCollider2D>();
float colliderPositionY = transform.position.y + thisCollider.offset.y;
return colliderPositionY - (thisCollider.size.y / 2);
}
}
......@@ -72,5 +72,5 @@ Material:
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 0.54901963, g: 0.28627452, b: 0.011764706, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment