- Home /
Moving Platform Collisions with Custom Movement Script
I am programming a platformer game and am having issues with moving platform collisions. HOWEVER, I am NOT using CharacterController. I am using a custom script from this tutorial: http://www.youtube.com/watch?annotation_id=annotation_299760&feature=iv&src_vid=d3HEFiDFApI&v=mN8alpNdTUk
and am having issues with moving platform scripts. Does anyone know of any scripts that might help me?
This script controls the player physics: using UnityEngine; using System.Collections;
[RequireComponent (typeof(BoxCollider))] public class PlayerPhysics : MonoBehaviour {
public LayerMask collisionMask;
private BoxCollider collider;
private Vector3 s;
private Vector3 c;
private float skin = 0.005f; //0.005f
[HideInInspector]
public bool grounded;
[HideInInspector]
public bool movementStopped;
Ray ray;
RaycastHit hit;
void Start() {
collider = GetComponent<BoxCollider>();
s = collider.size;
c = collider.center;
}
public void Move(Vector2 moveAmount) {
float deltaY = moveAmount.y;
float deltaX = moveAmount.x;
Vector2 p = transform.position;
// Check collisions above and below
grounded = false;
for (int i = 0; i<3; i ++) {
float dir = Mathf.Sign(deltaY);
float x = (p.x + c.x - s.x/2) + s.x/2 * i; // Left, centre and then rightmost point of collider
float y = p.y + c.y + s.y/2 * dir; // Bottom of collider
ray = new Ray(new Vector2(x,y), new Vector2(0,dir));
Debug.DrawRay(ray.origin,ray.direction);
if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaY) + skin,collisionMask)) {
// Get Distance between player and ground
float dst = Vector3.Distance (ray.origin, hit.point);
// Stop player's downwards movement after coming within skin width of a collider
if (dst > skin) {
deltaY = dst * dir - skin * dir;
}
else {
deltaY = 0;
}
grounded = true;
break;
}
}
// Check collisions left and right
movementStopped = false;
for (int i = 0; i<3; i ++) {
float dir = Mathf.Sign(deltaX);
float x = p.x + c.x + s.x/2 * dir; // Left, centre and then rightmost point of collider
float y = p.y + c.y - s.y/2 + s.y/2 * i; // Bottom of collider
ray = new Ray(new Vector2(x,y), new Vector2(dir,0));
Debug.DrawRay(ray.origin,ray.direction);
if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaX) + skin,collisionMask)) {
// Get Distance between player and ground
float dst = Vector3.Distance (ray.origin, hit.point);
// Stop player's downwards movement after coming within skin width of a collider
if (dst > skin) {
deltaX = dst * dir - skin * dir;
}
else {
deltaX = 0;
}
movementStopped = true;
break;
}
}
Vector2 finalTransform = new Vector2(deltaX,deltaY);
transform.Translate(finalTransform);
}
}
and this script controls the player's movement: using System.Collections;
[RequireComponent(typeof(PlayerPhysics))] public class PlayerController : MonoBehaviour {
// Player Handling
public float gravity = 20;
public float speed = 8;
public float acceleration = 30;
public float jumpHeight = 12;
private float currentSpeed;
private float targetSpeed;
private Vector2 amountToMove;
private PlayerPhysics playerPhysics;
void Start () {
playerPhysics = GetComponent<PlayerPhysics>();
}
void Update () {
// Reset acceleration upon collision
if (playerPhysics.movementStopped) {
targetSpeed = 0;
currentSpeed = 0;
}
// If player is touching the ground
if (playerPhysics.grounded) {
amountToMove.y = 0;
// Jump
if (Input.GetButtonDown("Jump")) {
amountToMove.y = jumpHeight;
}
}
// Input
targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);
// Set amount to move
amountToMove.x = currentSpeed;
amountToMove.y -= gravity * Time.deltaTime;
playerPhysics.Move(amountToMove * Time.deltaTime);
}
// Increase n towards target by speed
private float IncrementTowards(float n, float target, float a) {
if (n == target) {
return n;
}
else {
float dir = Mathf.Sign(target - n); // must n be increased or decreased to get closer to target
n += a * Time.deltaTime * dir;
return (dir == Mathf.Sign(target-n))? n: target; // if n has now passed target then return target, otherwise return n
}
}
}
Comment