Skip to content

Instantly share code, notes, and snippets.

@hcorion
Created May 13, 2016 05:04
Show Gist options
  • Save hcorion/0e831791c195635ce7561a7321c28e52 to your computer and use it in GitHub Desktop.
Save hcorion/0e831791c195635ce7561a7321c28e52 to your computer and use it in GitHub Desktop.
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;
// Use this for initialization
void Start ()
{
myRigidbody = gameObject.GetComponent<Rigidbody>();
capsule = gameObject.GetComponent<CapsuleCollider>();
distToGround = capsule.bounds.extents.y;
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown("Jump") && isGrounded())
{
myRigidbody.velocity = Vector3.up * verticalSpeed * 5;
}
if (currentPlatformPosition != 0)
//Debug.Log("Current Platform: " + (currentPlatformPosition - 1));
transform.LookAt(new Vector3(Platforms[currentPlatformPosition].transform.position.x, transform.position.y, Platforms[currentPlatformPosition].transform.position.z));
//Debug.DrawLine(transform.position, new Vector3(Platforms[currentPlatformPosition].transform.position.x, transform.position.y, Platforms[currentPlatformPosition].transform.position.z), Color.red, 0.01f, false);
}
void FixedUpdate()
{
//Moving towards positive X, positive Z = -yValue +xValue
//Moving towards positive X, negative Z = +yValue +xValue
//Moving towards negative X, negative Z = -yValue +xValue
//Moving towards negative X, positive Z = +yValue +xValue
float xValue;
Transform nextPlatformTransform = Platforms[currentPlatformPosition].transform;
//Xvalue is positive if the player is moving towards the postive X direction
//Xvalue is negative if the player is moving towards the negative X direction
if (nextPlatformTransform.position.x - transform.position.x >= 0)
xValue = nextPlatformTransform.position.x - transform.position.x;
else
xValue = transform.position.x - nextPlatformTransform.position.x;
//When the player is moving towards positve Z yValue is negative
//When the player is moving towards negative Z yValue is positive
float yValue = 0.0f;
/*if (transform.position.z - nextPlatformTransform.position.z <= 0)
yValue = transform.position.z - nextPlatformTransform.position.z;
else
yValue = transform.position.z - nextPlatformTransform.position.z;*/
//If moving towards negative Z
if (transform.position.z - nextPlatformTransform.position.z >= 0)
{
//If X is positive
if (nextPlatformTransform.position.x - transform.position.x >= 0)
yValue = transform.position.z - nextPlatformTransform.position.z;
//If X is negative
else
yValue = -(transform.position.z - nextPlatformTransform.position.z);
}
//If moving towards positive Z
else
{
if (nextPlatformTransform.position.x - transform.position.x >= 0)
yValue = transform.position.z - nextPlatformTransform.position.z;
else
yValue = -(transform.position.z - nextPlatformTransform.position.z);
}
//Debug.Log("yValue = " + yValue);
//Debug.Log("xValue = " + xValue);
//X operator AKA + or -
float xOP;
//Y operator AKA + or -
float yOP;
//Kind of hard to explain, sorry. The variable's name isn't very descriptive.
if (yValue >= 0)
{
yOP = 1;
}
else
{
yOP = -1;
}
xOP = 1;
if (currentPlatformPosition > 1)
{
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;
xOP = -1;
Debug.Log("Ahah! We did it!");
}
if(isBackwards == true)
{
xOP = -1;
Debug.Log("isBackwards is equal to true!");
}
//else
//{
//xOP = 1;
//}
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("Booyah!");
}
}
Debug.Log("Current Platform Position: " + currentPlatformPosition);
float angle = Mathf.Rad2Deg * Mathf.Atan(yOP * yValue / xValue);
if (transform.position.z - yValue > transform.position.z)
{
maxAngle = 90;
}
else if (transform.position.z - yValue < transform.position.z)
{
maxAngle = 180;
}
//Debug.Log("Angle = " + angle);
//myRigidbody.velocity = new Vector3( ((-(angle / 90 - 1)) * horizontalSpeed) * Input.GetAxis("Horizontal"), myRigidbody.velocity.y,(angle / 90 * horizontalSpeed) * Input.GetAxis("Horizontal"));
//myRigidbody.velocity = new Vector3(((-(angle / maxAngle - 1)) * horizontalSpeed) * Input.GetAxis("Horizontal"), myRigidbody.velocity.y, (angle / maxAngle * horizontalSpeed) * Input.GetAxis("Horizontal"));
myRigidbody.velocity = new Vector3(xOP * (-(angle/90 - 1) * horizontalSpeed * (Input.GetAxis("Horizontal"))),
myRigidbody.velocity.y,
-yOP*((angle / 90) * horizontalSpeed * Input.GetAxis("Horizontal"))*xOP);
}
//Taken from here: http://dotnet-snippets.com/snippet/euclidean-algorithm-for-gcd/
int GCD(int a, int b)
{
while (a != 0 && b != 0)
{
if (a > b)
a %= b;
else
b %= a;
}
if (a == 0)
return b;
else
return a;
}
bool isGrounded()
{
//bool raycast = Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
}
void OnCollisionEnter(Collision collision)
{
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