Created
May 26, 2020 00:14
-
-
Save jadowdl/3b91afb048e777c55a7075012a981f5a 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class BallController : MonoBehaviour | |
{ | |
public float acceleration; | |
public CameraController2 camera; | |
private Rigidbody rigidBody; | |
private KeyCode[] inputKeys; | |
private Vector3[] directionsForKeys; | |
private bool canJump = true; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
inputKeys = new KeyCode[] { KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D }; | |
directionsForKeys = new Vector3[] { Vector3.forward, Vector3.left, Vector3.back, Vector3.right }; | |
rigidBody = GetComponent<Rigidbody>(); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
} | |
void FixedUpdate() | |
{ | |
// Will pull the ball in the "xward" direction. | |
// Vector3 tmp = rigidBody.velocity; | |
// tmp.x += .1f * Time.deltaTime; | |
// rigidBody.velocity = tmp; | |
for (int i = 0; i < inputKeys.Length; i++) | |
{ | |
var key = inputKeys[i]; | |
if (Input.GetKey(key)) | |
{ | |
Vector3 lookedAt = -camera.GetCameraDelta(); | |
lookedAt.y = 0; | |
lookedAt.Normalize(); | |
// Debug.DrawRay(transform.position, lookedAt * 10, Color.magenta, 0, false); | |
Vector3 dir = Quaternion.LookRotation(directionsForKeys[i]) * lookedAt; | |
Vector3 movement = dir * acceleration * Time.deltaTime; | |
rigidBody.AddForce(movement); | |
// Debug.DrawRay(transform.position, movement, Color.cyan, 0, false); | |
} | |
if (Input.GetKey(KeyCode.Space)) { | |
if (canJump) { | |
rigidBody.AddForce(Vector3.up * 10, ForceMode.Impulse); | |
} | |
canJump = false; | |
} else { | |
//canJump = true; | |
} | |
} | |
} | |
//make sure u replace "floor" with your gameobject name.on which player is standing | |
void OnCollisionEnter(Collision theCollision) | |
{ | |
Debug.Log(">>" + theCollision.gameObject.name); | |
if ("Ground".Equals(theCollision.gameObject.tag)) | |
{ | |
canJump = true; | |
} | |
} | |
//consider when character is jumping .. it will exit collision. | |
void OnCollisionExit(Collision theCollision) | |
{ | |
Debug.Log("<<" + theCollision.gameObject.name); | |
if ("Ground".Equals(theCollision.gameObject.tag)) | |
{ | |
canJump = false; | |
} | |
} | |
void OnTriggerEnter(Collider other) | |
{ | |
Debug.Log("T>>" + other.gameObject.name); | |
} | |
//When the Primitive exits the collision, it will change Color | |
void OnTriggerExit(Collider other) | |
{ | |
Debug.Log("T<<" + other.gameObject.name); | |
} | |
private void LateUpdate() | |
{ | |
} | |
} | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class BallController : MonoBehaviour | |
{ | |
public float acceleration; | |
public CameraController2 camera; | |
private Rigidbody rigidBody; | |
private KeyCode[] inputKeys; | |
private Vector3[] directionsForKeys; | |
private bool canJump = true; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
inputKeys = new KeyCode[] { KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D }; | |
directionsForKeys = new Vector3[] { Vector3.forward, Vector3.left, Vector3.back, Vector3.right }; | |
rigidBody = GetComponent<Rigidbody>(); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
} | |
void FixedUpdate() | |
{ | |
// Will pull the ball in the "xward" direction. | |
// Vector3 tmp = rigidBody.velocity; | |
// tmp.x += .1f * Time.deltaTime; | |
// rigidBody.velocity = tmp; | |
for (int i = 0; i < inputKeys.Length; i++) | |
{ | |
var key = inputKeys[i]; | |
if (Input.GetKey(key)) | |
{ | |
Vector3 lookedAt = -camera.GetCameraDelta(); | |
lookedAt.y = 0; | |
lookedAt.Normalize(); | |
// Debug.DrawRay(transform.position, lookedAt * 10, Color.magenta, 0, false); | |
Vector3 dir = Quaternion.LookRotation(directionsForKeys[i]) * lookedAt; | |
Vector3 movement = dir * acceleration * Time.deltaTime; | |
rigidBody.AddForce(movement); | |
// Debug.DrawRay(transform.position, movement, Color.cyan, 0, false); | |
} | |
if (Input.GetKey(KeyCode.Space)) { | |
if (canJump) { | |
rigidBody.AddForce(Vector3.up * 10, ForceMode.Impulse); | |
} | |
canJump = false; | |
} else { | |
//canJump = true; | |
} | |
} | |
} | |
//make sure u replace "floor" with your gameobject name.on which player is standing | |
void OnCollisionEnter(Collision theCollision) | |
{ | |
Debug.Log(">>" + theCollision.gameObject.name); | |
if ("Ground".Equals(theCollision.gameObject.tag)) | |
{ | |
canJump = true; | |
} | |
} | |
//consider when character is jumping .. it will exit collision. | |
void OnCollisionExit(Collision theCollision) | |
{ | |
Debug.Log("<<" + theCollision.gameObject.name); | |
if ("Ground".Equals(theCollision.gameObject.tag)) | |
{ | |
canJump = false; | |
} | |
} | |
void OnTriggerEnter(Collider other) | |
{ | |
Debug.Log("T>>" + other.gameObject.name); | |
} | |
//When the Primitive exits the collision, it will change Color | |
void OnTriggerExit(Collider other) | |
{ | |
Debug.Log("T<<" + other.gameObject.name); | |
} | |
private void LateUpdate() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment