Changing player movement

This commit is contained in:
Anonymous Raccoon
2018-03-05 18:12:20 +01:00
parent 5678b9768a
commit 53a218b727
3 changed files with 244 additions and 234 deletions

View File

@@ -1,16 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
public class MovingElement : MonoBehaviour {
[RequireComponent(typeof(Rigidbody))]
public class MovingElement : MonoBehaviour
{
[SerializeField] private Vector3 position1;
[SerializeField] private Vector3 position2;
[Space]
[SerializeField] private float speed = 1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
private Vector3 middlePosition;
private float intensityX;
private float intensityY;
[HideInInspector] public Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.useGravity = false;
rb.isKinematic = true;
middlePosition = (position1 + position2) / 2;
rb.position = middlePosition;
intensityX = position1.x - rb.position.x;
intensityY = position1.y - rb.position.y;
}
private void FixedUpdate ()
{
rb.MovePosition(new Vector3(middlePosition.x + Mathf.Sin(Time.time * speed) * intensityX, middlePosition.y + Mathf.Sin(Time.time * speed) * intensityY, rb.position.z));
}
}

View File

@@ -12,15 +12,12 @@ public class PlayerMovement : MonoBehaviour
[SerializeField] private int wallJumpPush = 250;
[Space]
[SerializeField] private LayerMask groundMask;
[SerializeField] private LayerMask wallMask;
private bool isGrounded = true;
private bool canCancel = false;
private bool canWallJump = false;
private bool groundedLastFrame = false;
private int wallDirection;
private float jumpDirection;
private bool airControl = false;
private MovingElement movingPlateform;
private Rigidbody rb;
@@ -30,80 +27,142 @@ public class PlayerMovement : MonoBehaviour
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
private bool IsGrounded()
{
if (Physics.OverlapSphere(rb.position, 1.5f, groundMask).Length > 0)
if (Mathf.Abs(RelativeVelocity().y) < 0.1f)
{
isGrounded = true;
canCancel = false;
if (groundedLastFrame)
return true;
else
{
groundedLastFrame = true;
return false;
}
}
else
isGrounded = false;
groundedLastFrame = false;
return false;
}
private bool IsSliding()
{
Collider[] walls = Physics.OverlapSphere(rb.position, 1, wallMask);
if (walls.Length > 0)
{
canWallJump = true;
if(walls[0].transform.position.x > rb.position.x)
wallDirection = -1;
else
if (walls[0].transform.position.x > rb.position.x && Input.GetAxis("Horizontal") > 0)
{
wallDirection = 1;
return true;
}
else if(walls[0].transform.position.x < rb.position.x && Input.GetAxis("Horizontal") < 0)
{
wallDirection = -1;
return true;
}
}
else
{
canWallJump = false;
}
return false;
}
private bool AirControl(bool isGrounded)
{
if (!isGrounded)
{
if (jumpDirection > 0)
{
if (Input.GetAxis("Horizontal") > 0)
airControl = false;
return false;
else
airControl = true;
return true;
}
else
{
if (Input.GetAxis("Horizontal") > 0)
airControl = true;
return true;
else
airControl = false;
return false;
}
}
return false;
}
rb.MovePosition(rb.position + new Vector3(Input.GetAxis("Horizontal") * (airControl ? airSpeed : speed) * Time.fixedDeltaTime - rb.velocity.x, 0, 0));
airControl = false;
public void OnCollisionEnter(Collision collision)
{
MovingElement movingElement = collision.gameObject.GetComponent<MovingElement>();
if(movingElement != null)
{
movingPlateform = movingElement;
}
}
private Vector3 RelativeVelocity()
{
return rb.velocity - PlateformVelocity();
}
private Vector3 PlateformVelocity()
{
if (movingPlateform != null)
return movingPlateform.rb.velocity;
else
return Vector3.zero;
}
private void FixedUpdate()
{
bool isGrounded = IsGrounded();
bool isSliding = IsSliding();
bool airControl = AirControl(isGrounded);
//Check if we have left the plateform
if (movingPlateform != null && !isGrounded && !groundedLastFrame && !isSliding)
movingPlateform = null;
//Move user with horizontal axis input
rb.AddForce(new Vector3(Input.GetAxis("Horizontal") * (airControl ? airSpeed : speed) - rb.velocity.x, 0, 0), ForceMode.Impulse);
//Make user jump
if (Input.GetButtonDown("Jump") && isGrounded)
{
jumpDirection = Input.GetAxis("Horizontal");
isGrounded = false;
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
canCancel = true;
rb.velocity = new Vector3(rb.velocity.x, jumpForce + (movingPlateform != null ? movingPlateform.rb.velocity.y : 0) , rb.velocity.z);
}
if (Input.GetButtonUp("Jump") && !isGrounded && rb.velocity.y > smallJump && canCancel)
//Make a small jump if user drop the button
if (Input.GetButtonUp("Jump") && !isGrounded && rb.velocity.y > smallJump)
{
canCancel = false;
rb.velocity = new Vector3(rb.velocity.x, smallJump, rb.velocity.z);
}
//Move with the plateform
if (movingPlateform != null)
rb.AddForce(new Vector3(movingPlateform.rb.velocity.x, 0, 0), ForceMode.Impulse);
if (rb.velocity.y < 1)
//Apply more gravity
if (rb.velocity.y < 1 && !isSliding)
{
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y - gravity * Time.fixedDeltaTime, rb.velocity.z);
rb.AddForce(new Vector3(0, gravity, 0), ForceMode.Acceleration);
}
if (canWallJump && !isGrounded && Input.GetButtonDown("Jump"))
//WallSlide
if (isSliding)
{
print("Wall Jumping");
canCancel = false;
jumpDirection = wallDirection;
rb.velocity = new Vector3(wallJumpPush * -wallDirection, wallJump, rb.velocity.z);
//rb.AddForce(new Vector3(wallJumpPush * -wallDirection, wallJump, 0), ForceMode.Acceleration);
rb.AddForce(new Vector3(0, Mathf.Abs(rb.velocity.y) + gravity / 3, 0), ForceMode.Acceleration);
//Wall Jump
if (Input.GetButtonDown("Jump"))
{
print("Wall jump");
jumpDirection = -wallDirection;
rb.AddForce(new Vector3(wallJumpPush * jumpDirection, wallJump, 0), ForceMode.Impulse);
}
}
//////Wall jump
//if (canWallJump && !isGrounded && Input.GetButtonDown("Jump"))
//{
// print("Wall Jumping");
// //canCancel = false;
// jumpDirection = wallDirection;
// //rb.velocity = new Vector3(wallJumpPush * -wallDirection, wallJump, rb.velocity.z);
// rb.AddForce(new Vector3(wallJumpPush * -wallDirection, wallJump, 0), ForceMode.Acceleration);
//}
}
}

View File

@@ -188,7 +188,7 @@ Transform:
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 5749160}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1, z: -12.6}
m_LocalPosition: {x: 0, y: 4.6, z: -15.65}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
@@ -276,174 +276,8 @@ Transform:
m_LocalScale: {x: 30, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 6
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 90}
--- !u!1001 &353878745
Prefab:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 4620220062861480, guid: 1e59ce4d0aab18f44a51b286de5259c4, type: 2}
propertyPath: m_LocalPosition.x
value: -4.4333916
objectReference: {fileID: 0}
- target: {fileID: 4620220062861480, guid: 1e59ce4d0aab18f44a51b286de5259c4, type: 2}
propertyPath: m_LocalPosition.y
value: 3.46
objectReference: {fileID: 0}
- target: {fileID: 4620220062861480, guid: 1e59ce4d0aab18f44a51b286de5259c4, type: 2}
propertyPath: m_LocalPosition.z
value: 0.176
objectReference: {fileID: 0}
- target: {fileID: 4620220062861480, guid: 1e59ce4d0aab18f44a51b286de5259c4, type: 2}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4620220062861480, guid: 1e59ce4d0aab18f44a51b286de5259c4, type: 2}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4620220062861480, guid: 1e59ce4d0aab18f44a51b286de5259c4, type: 2}
propertyPath: m_LocalRotation.z
value: -1
objectReference: {fileID: 0}
- target: {fileID: 4620220062861480, guid: 1e59ce4d0aab18f44a51b286de5259c4, type: 2}
propertyPath: m_LocalRotation.w
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4620220062861480, guid: 1e59ce4d0aab18f44a51b286de5259c4, type: 2}
propertyPath: m_RootOrder
value: 4
objectReference: {fileID: 0}
- target: {fileID: 136123792039155002, guid: 1e59ce4d0aab18f44a51b286de5259c4,
type: 2}
propertyPath: m_Material
value:
objectReference: {fileID: 13400000, guid: 3f67cbc34e2adef4e808c3445e69a63d,
type: 2}
- target: {fileID: 65416904971122956, guid: 1e59ce4d0aab18f44a51b286de5259c4,
type: 2}
propertyPath: m_Material
value:
objectReference: {fileID: 13400000, guid: 3f67cbc34e2adef4e808c3445e69a63d,
type: 2}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: 1e59ce4d0aab18f44a51b286de5259c4, type: 2}
m_IsPrefabParent: 0
--- !u!1001 &1063534285
Prefab:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 4061937932440146, guid: b21e2e91c2466fa4ba8c38be7831894c, type: 2}
propertyPath: m_LocalPosition.x
value: -4.4333916
objectReference: {fileID: 0}
- target: {fileID: 4061937932440146, guid: b21e2e91c2466fa4ba8c38be7831894c, type: 2}
propertyPath: m_LocalPosition.y
value: 3.024
objectReference: {fileID: 0}
- target: {fileID: 4061937932440146, guid: b21e2e91c2466fa4ba8c38be7831894c, type: 2}
propertyPath: m_LocalPosition.z
value: 0.176
objectReference: {fileID: 0}
- target: {fileID: 4061937932440146, guid: b21e2e91c2466fa4ba8c38be7831894c, type: 2}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4061937932440146, guid: b21e2e91c2466fa4ba8c38be7831894c, type: 2}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4061937932440146, guid: b21e2e91c2466fa4ba8c38be7831894c, type: 2}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4061937932440146, guid: b21e2e91c2466fa4ba8c38be7831894c, type: 2}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4061937932440146, guid: b21e2e91c2466fa4ba8c38be7831894c, type: 2}
propertyPath: m_RootOrder
value: 3
objectReference: {fileID: 0}
- target: {fileID: 4061937932440146, guid: b21e2e91c2466fa4ba8c38be7831894c, type: 2}
propertyPath: m_LocalScale.y
value: 1
objectReference: {fileID: 0}
- target: {fileID: 65008393815429252, guid: b21e2e91c2466fa4ba8c38be7831894c,
type: 2}
propertyPath: m_Material
value:
objectReference: {fileID: 13400000, guid: 3f67cbc34e2adef4e808c3445e69a63d,
type: 2}
- target: {fileID: 136665385331263236, guid: b21e2e91c2466fa4ba8c38be7831894c,
type: 2}
propertyPath: m_Material
value:
objectReference: {fileID: 13400000, guid: 3f67cbc34e2adef4e808c3445e69a63d,
type: 2}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: b21e2e91c2466fa4ba8c38be7831894c, type: 2}
m_IsPrefabParent: 0
--- !u!1001 &1117494753
Prefab:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 4928226377463138, guid: bebad6dbaee6f9440afb6cfabba013d3, type: 2}
propertyPath: m_LocalPosition.x
value: -4.61
objectReference: {fileID: 0}
- target: {fileID: 4928226377463138, guid: bebad6dbaee6f9440afb6cfabba013d3, type: 2}
propertyPath: m_LocalPosition.y
value: 3.2319999
objectReference: {fileID: 0}
- target: {fileID: 4928226377463138, guid: bebad6dbaee6f9440afb6cfabba013d3, type: 2}
propertyPath: m_LocalPosition.z
value: -0.285
objectReference: {fileID: 0}
- target: {fileID: 4928226377463138, guid: bebad6dbaee6f9440afb6cfabba013d3, type: 2}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4928226377463138, guid: bebad6dbaee6f9440afb6cfabba013d3, type: 2}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4928226377463138, guid: bebad6dbaee6f9440afb6cfabba013d3, type: 2}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4928226377463138, guid: bebad6dbaee6f9440afb6cfabba013d3, type: 2}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4928226377463138, guid: bebad6dbaee6f9440afb6cfabba013d3, type: 2}
propertyPath: m_RootOrder
value: 2
objectReference: {fileID: 0}
- target: {fileID: 4928226377463138, guid: bebad6dbaee6f9440afb6cfabba013d3, type: 2}
propertyPath: m_LocalScale.x
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4928226377463138, guid: bebad6dbaee6f9440afb6cfabba013d3, type: 2}
propertyPath: m_LocalScale.y
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4928226377463138, guid: bebad6dbaee6f9440afb6cfabba013d3, type: 2}
propertyPath: m_LocalScale.z
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: bebad6dbaee6f9440afb6cfabba013d3, type: 2}
m_IsPrefabParent: 0
--- !u!1 &1180913636
GameObject:
m_ObjectHideFlags: 0
@@ -539,11 +373,11 @@ Transform:
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1180913636}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: -4.24, y: 4.34, z: 0}
m_LocalPosition: {x: -1, y: 4.9, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 8
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1180913642
MonoBehaviour:
@@ -560,15 +394,13 @@ MonoBehaviour:
airSpeed: 7
jumpForce: 8
smallJump: 4
gravity: 8
gravity: -10
wallJump: 4
wallJumpPush: 2
groundMask:
serializedVersion: 2
m_Bits: 256
wallMask:
serializedVersion: 2
m_Bits: 512
movingPlateform: {fileID: 0}
--- !u!1 &1200947730
GameObject:
m_ObjectHideFlags: 0
@@ -577,11 +409,12 @@ GameObject:
serializedVersion: 5
m_Component:
- component: {fileID: 1200947735}
- component: {fileID: 1200947736}
- component: {fileID: 1200947734}
- component: {fileID: 1200947733}
- component: {fileID: 1200947732}
- component: {fileID: 1200947731}
m_Layer: 9
m_Layer: 8
m_Name: Moving Cube
m_TagString: Untagged
m_Icon: {fileID: 0}
@@ -599,9 +432,9 @@ Rigidbody:
m_Drag: 0
m_AngularDrag: 0.05
m_UseGravity: 1
m_IsKinematic: 0
m_IsKinematic: 1
m_Interpolate: 0
m_Constraints: 0
m_Constraints: 112
m_CollisionDetection: 0
--- !u!23 &1200947732
MeshRenderer:
@@ -663,12 +496,27 @@ Transform:
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1200947730}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -10.42, y: 3.4, z: 0}
m_LocalPosition: {x: 1.17, y: 10.52, z: 0}
m_LocalScale: {x: 3, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 7
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1200947736
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1200947730}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 49ea6d0c92f14454aa950f9902c7e3ed, type: 3}
m_Name:
m_EditorClassIdentifier:
position1: {x: -10.42, y: 2.34, z: 0}
position2: {x: 1.17, y: 11.8, z: 0}
speed: 1
rb: {fileID: 0}
--- !u!1 &1699708122
GameObject:
m_ObjectHideFlags: 0
@@ -766,7 +614,7 @@ MeshRenderer:
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_Materials:
- {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 2100000, guid: 0f30549d71daee342b20e581eab82e60, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
@@ -816,5 +664,89 @@ Transform:
m_LocalScale: {x: 30, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2074438684
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 2074438688}
- component: {fileID: 2074438687}
- component: {fileID: 2074438686}
- component: {fileID: 2074438685}
m_Layer: 8
m_Name: Plateform
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!23 &2074438685
MeshRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2074438684}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_Materials:
- {fileID: 2100000, guid: 0f30549d71daee342b20e581eab82e60, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 1
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 0
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!65 &2074438686
BoxCollider:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2074438684}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!33 &2074438687
MeshFilter:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2074438684}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!4 &2074438688
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2074438684}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -3.71, y: 3.02, z: 0}
m_LocalScale: {x: 6.84, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}