Created
May 13, 2016 05:03
-
-
Save hcorion/592422ad8edc9c9a1147013a0e4803d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class MovingScriptNew : MonoBehaviour { | |
public float horizontalSpeed = 4; | |
public float verticalSpeed = 2; | |
private float verticalMovement; | |
private float playerHorizontalMovement; | |
private Rigidbody myRigidbody; | |
private float distToGround; | |
private CapsuleCollider capsule; | |
private RaycastHit hit; | |
bool isBackwards = false; | |
Transform currentLookRotation; | |
private int currentPlatformPosition; | |
public GameObject[] Platforms; | |
//Test script | |
int velocity = 11; | |
GameObject sphere; | |
float angle; | |
int maxAngle; | |
private float jumpVal; | |
//Variables related to jumping | |
private float maxJumpHeight = 25; | |
private float jumpHeight = 0; | |
public UnityEngine.UI.Slider devJumpHeightSlider; | |
void Start () | |
{ | |
//Initializing components, it is assumed they are children of the attached objects. | |
myRigidbody = gameObject.GetComponent<Rigidbody>(); | |
capsule = gameObject.GetComponent<CapsuleCollider>(); | |
//Used in InGrounded, which prevents mid-air jumps. | |
distToGround = capsule.bounds.extents.y; | |
//Used to test a good jumping distance. | |
devJumpHeightSlider.value = maxJumpHeight; | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
maxJumpHeight = devJumpHeightSlider.value; | |
//Use GetButton not GetButtonDown! | |
if (Input.GetButton("Jump")) | |
{ | |
jumpHeight++; | |
if (maxJumpHeight > jumpHeight) | |
{ | |
//multiplied by 10 gets you a decent jump, when verticalSpeed is set to 2. | |
myRigidbody.AddRelativeForce(Vector3.up * verticalSpeed * 10); | |
} | |
} | |
//myRigidbody.AddForce(Vector3.up * verticalSpeed * 5); | |
if (currentPlatformPosition != 0) | |
transform.LookAt(new Vector3(Platforms[currentPlatformPosition].transform.position.x, transform.position.y, Platforms[currentPlatformPosition].transform.position.z)); | |
} | |
void FixedUpdate() | |
{ | |
Transform nextPlatformTransform = Platforms[currentPlatformPosition].transform; | |
//xValue is the distance between the player and the next platform on the X axis. | |
float xValue = nextPlatformTransform.position.x - transform.position.x; | |
//zValue is the distance between the player and the next platform on the Z axis. | |
float zValue = transform.position.z - nextPlatformTransform.position.z; | |
if (xValue < 0) | |
{ | |
//If xValue is negative, make it positive. | |
xValue = -xValue; | |
//If xValue is negative, and if zValue is negative it will make zValue positive. | |
//If xValue is negative, and if zValue is positive it will make zValue negative. | |
zValue = -zValue; | |
} | |
//X operator AKA + or - | |
float xOP = 1; | |
//Y operator AKA + or - | |
float yOP; | |
//yOP changes every time the player starts moving towards a different Z or X axis. | |
if (zValue >= 0) | |
{ | |
yOP = 1; | |
} | |
else | |
{ | |
yOP = -1; | |
} | |
//If we aren't at the starting platform, then... | |
if (currentPlatformPosition > 1) | |
{ | |
//If the next platform is towards the negative X axis and the player was moving towards the positive X then... | |
if (Platforms[currentPlatformPosition - 2].transform.position.x < Platforms[currentPlatformPosition - 1].transform.position.x && | |
Platforms[currentPlatformPosition].transform.position.x < Platforms[currentPlatformPosition - 1].transform.position.x && | |
isBackwards == false) | |
{ | |
isBackwards = true; | |
} | |
if(isBackwards == true) | |
{ | |
xOP = -1; | |
} | |
//If the next platform is towards the positive X axis and the player was moving towards the negative X then... | |
if (Platforms[currentPlatformPosition - 2].transform.position.x > Platforms[currentPlatformPosition - 1].transform.position.x && | |
Platforms[currentPlatformPosition].transform.position.x > Platforms[currentPlatformPosition - 1].transform.position.x && | |
isBackwards == true) | |
{ | |
isBackwards = false; | |
xOP = 1; | |
} | |
} | |
//Debug.Log("Current Platform Position: " + currentPlatformPosition); | |
float angle = Mathf.Rad2Deg * Mathf.Atan(yOP * zValue / xValue); | |
if (transform.position.z - zValue > transform.position.z) | |
{ | |
maxAngle = 90; | |
} | |
else if (transform.position.z - zValue < transform.position.z) | |
{ | |
maxAngle = 180; | |
} | |
myRigidbody.velocity = new Vector3(xOP * (-(angle/90 - 1) * horizontalSpeed * (Input.GetAxis("Horizontal"))), | |
myRigidbody.velocity.y, | |
-yOP*((angle / 90) * horizontalSpeed * Input.GetAxis("Horizontal"))*xOP); | |
} | |
bool IsGrounded() | |
{ | |
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f); | |
} | |
void OnCollisionEnter(Collision collision) | |
{ | |
jumpHeight = 0; | |
for(int i= 0; i <= Platforms.Length - 1; i++) | |
{ | |
if(Platforms[i] == collision.gameObject) | |
{ | |
currentPlatformPosition = i+1; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment