Skip to content

Instantly share code, notes, and snippets.

@gleydson
Last active April 13, 2018 20:30
Show Gist options
  • Save gleydson/138fe7bd473436de880557c92439c965 to your computer and use it in GitHub Desktop.
Save gleydson/138fe7bd473436de880557c92439c965 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBehaviourScript : MonoBehaviour {
private float x;
private float y;
private float z;
public bool canJump = true;
public float velocity = 0.2f;
public float rotationVelocity = 1.3f;
public float force = 0.5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
x = Input.GetAxis("Horizontal") * rotationVelocity;
z = Input.GetAxis("Vertical") * velocity;
transform.Translate(0, 0, z);
transform.Rotate(0, x, 0);
y = Input.GetAxis ("Jump") * force;
transform.Translate (0, y, 0);
}
/*
void OnCollisionStay(Collision col) {
if (col.gameObject.tag.Equals ("Plataform") || col.gameObject.tag.Equals ("floor")) {
canJump = true;
}
}
void OnCollisionExit(Collision col) {
if (col.gameObject.tag.Equals ("Plataform") || col.gameObject.tag.Equals ("floor")) {
canJump = false;
}
}*/
}
@gleydson
Copy link
Author

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerBehaviourScript : MonoBehaviour
{

private float x;
private float y;
private float z;
public bool canJump;

public float velocity = 0.2f;
public float rotationVelocity = 1.3f;
public float force = 0.5f;
private Rigidbody rigidbody;

// Use this for initialization
void Start() {
    canJump = true;
    rigidbody = GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update() {
    x = Input.GetAxis("Horizontal") * rotationVelocity;
    y = Input.GetAxis("Jump") * force;
    z = Input.GetAxis("Vertical") * velocity;
    float z2 = Input.GetAxis("Vertical") * force;

    transform.Rotate(0, x, 0);

    if (canJump) {
        transform.Translate(0, 0, z);
       
        if (Input.GetKeyDown(KeyCode.Space))
        {
           // Vector3 myForward = transform.TransformDirection(Vector3.forward);
           // rigidbody.AddForce(myForward * force);
            //rigidbody.velocity = rigidbody.velocity + new Vector3(0, (y / 1.5f), (z2 / 1.5f));
            rigidbody.AddForce((transform.TransformDirection(Vector3.forward) + transform.TransformDirection(Vector3.up)) * force);
            Debug.Log("Z = "+ z2 + "\n Y = " + y);

        }
    }
    

    
    //transform.Translate(0, 0, z);
}

void OnCollisionEnter(Collision col) {
	if (col.gameObject.tag.Equals ("Plataform") || col.gameObject.tag.Equals ("Floor")) {
		canJump = true;
        Debug.Log("Entrou!");
	}
}

void OnCollisionExit(Collision col) {
	if (col.gameObject.tag.Equals ("Plataform") || col.gameObject.tag.Equals ("Floor")) {
		canJump = false;
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment