init
This commit is contained in:
30
Assets/Scripts/CameraFollower.cs
Normal file
30
Assets/Scripts/CameraFollower.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraFollower : MonoBehaviour
|
||||
{
|
||||
public static CameraFollower instance;
|
||||
void Awake(){
|
||||
instance = this;
|
||||
}
|
||||
public float Xsmoothness =1f;
|
||||
public float Ysmoothness =1f;
|
||||
public static Transform Target;
|
||||
public Vector2 offset;
|
||||
void FixedUpdate()
|
||||
{
|
||||
UpdateFrame();
|
||||
}
|
||||
|
||||
public static void UpdateFrame(){
|
||||
instance.updateFrame();
|
||||
}
|
||||
|
||||
|
||||
void updateFrame(){
|
||||
float newX = Mathf.Lerp(transform.position.x, Target.position.x + offset.x, Xsmoothness);
|
||||
float newY = Mathf.Lerp(transform.position.y, Target.position.y + offset.y, Ysmoothness);
|
||||
transform.position = new Vector3(newX, newY, transform.position.z);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/CameraFollower.cs.meta
Normal file
11
Assets/Scripts/CameraFollower.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aaa9dff48f2e7b34a9e8d38dae26b636
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
52
Assets/Scripts/PlayerController.cs
Normal file
52
Assets/Scripts/PlayerController.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
public float groundCheckDistance = 0.1f;
|
||||
public float gravity;
|
||||
public float maxGravityVelocity= 0.01f;
|
||||
public Vector3 velocity = Vector3.zero;
|
||||
public Vector3 speed;
|
||||
public Vector3 maxVelocity;
|
||||
public float friction = 0.1f;
|
||||
void Start()
|
||||
{
|
||||
CameraFollower.Target=transform;
|
||||
}
|
||||
|
||||
float input =0;
|
||||
void Update(){
|
||||
input = Input.GetAxis("Horizontal");
|
||||
}
|
||||
void FixedUpdate()
|
||||
{
|
||||
Gravity();
|
||||
HandleInput();
|
||||
transform.eulerAngles = Vector3.zero + new Vector3(0,0, (-velocity.x / maxVelocity.x) * 10);
|
||||
transform.position += velocity;
|
||||
}
|
||||
|
||||
void HandleInput(){
|
||||
if(input >0){
|
||||
velocity = new Vector3(Mathf.Clamp(velocity.x + speed.x, -maxVelocity.x, maxVelocity.x), Mathf.Clamp(velocity.y + speed.y, -maxVelocity.y, maxVelocity.y));
|
||||
}else if(input < 0){
|
||||
velocity = new Vector3(Mathf.Clamp(velocity.x - speed.x, -maxVelocity.x, maxVelocity.x), Mathf.Clamp(velocity.y + speed.y, -maxVelocity.y, maxVelocity.y));
|
||||
}else{
|
||||
velocity = Vector3.Lerp(velocity, new Vector3(0, (velocity.y > 0) ? 0 : velocity.y),friction);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Gravity(){
|
||||
RaycastHit2D groundHit = Physics2D.Linecast(transform.position, transform.position - new Vector3(0,groundCheckDistance,0));
|
||||
if(groundHit.collider != null){
|
||||
Debug.DrawLine(transform.position, groundHit.point, Color.green);
|
||||
float dist = transform.position.y - groundHit.point.y;
|
||||
velocity = new Vector3(velocity.x,groundCheckDistance - dist,velocity.z);
|
||||
}else{
|
||||
velocity = new Vector3(velocity.x,Mathf.Lerp(velocity.y, -maxGravityVelocity,gravity),velocity.z);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PlayerController.cs.meta
Normal file
11
Assets/Scripts/PlayerController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b767c5c348036d54a93556239488e790
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user