Skip to content

Instantly share code, notes, and snippets.

@bzgeb
Created September 14, 2012 18:39
Show Gist options
  • Save bzgeb/3723838 to your computer and use it in GitHub Desktop.
Save bzgeb/3723838 to your computer and use it in GitHub Desktop.
Stone flick controls
using UnityEngine;
using System.Collections;
public class StoneControls : MonoBehaviour {
public Object stone_prefab;
public Vector2 sensitivity;
public Collider lake_collider;
private Vector3 r_start;
private Vector3 r_end;
private float throw_multiplier = 1;
private float throw_multiplier_fudge = 0.6f;
private float throw_strength = 2000;
private float start_time;
private Vector2 start_pos;
private bool is_swipe;
private float comfort_zone = 22f;
private float min_swipe_dist = 0.01f;
private float max_swipe_time = 0.44f;
private float min_swipe_time = 0.01f;
private bool accurate_aim = true;
Ray s_ray;
Ray e_ray;
// Update is called once per frame
void Update ()
{
if ( Input.GetButtonDown("Jump"))
{
throw_multiplier = 1.0f;
// throw_stone();
}
if ( Input.GetMouseButtonDown(0) )
{
s_ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
lake_collider.Raycast(s_ray, out hit, 1000);
r_start = hit.point;
}
if ( Input.GetMouseButtonUp(0) )
{
e_ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
lake_collider.Raycast(e_ray, out hit, 1000);
r_end = hit.point;
}
Debug.DrawRay(s_ray.origin, s_ray.direction * 1000, Color.yellow);
Debug.DrawRay(e_ray.origin, e_ray.direction * 1000, Color.yellow);
Debug.DrawLine(r_start, r_end);
#if UNITY_IPHONE
if ( Input.touches.Length == 0 ) return;
Touch touch = Input.touches[0];
switch ( touch.phase )
{
case TouchPhase.Began:
is_swipe = true;
start_pos = touch.position;
start_time = Time.time;
s_ray = Camera.main.ScreenPointToRay(touch.position);
break;
case TouchPhase.Stationary:
is_swipe = false;
break;
case TouchPhase.Moved:
// if (Mathf.Abs(touch.position.x - start_pos.x) > comfort_zone) {
// is_swipe = false;
// }
break;
case TouchPhase.Ended:
float swipe_time = Time.time - start_time;
float swipe_dist = (touch.position.y - start_pos.y) / Screen.height;
e_ray = Camera.main.ScreenPointToRay(touch.position);
Vector2 swipe_vector = (touch.position - start_pos).normalized;
float swipe_angle = ((Mathf.Atan2(swipe_vector.y, swipe_vector.x) * Mathf.Rad2Deg) - 90) * 0.5f;
// if (Mathf.Abs(swipe_angle) > comfort_zone) {
// is_swipe = false;
// }
Matrix4x4 rot_matrix = Matrix4x4.identity;
rot_matrix.SetTRS(Vector3.zero, Quaternion.AngleAxis(-swipe_angle, Vector3.up), new Vector3(1, 1, 1));
print("Time: " + swipe_time);
print("Distance: " + swipe_dist);
print("D/T " + swipe_dist / swipe_time);
print("Swipe Vector: " + swipe_vector);
print("Angle :" + swipe_angle);
if (swipe_time > max_swipe_time) return;
if (swipe_dist < min_swipe_dist) return;
swipe_time = Mathf.Max(min_swipe_time, swipe_time);
throw_multiplier = (swipe_dist / swipe_time) * throw_multiplier_fudge;
// print("Multiplier: " + throw_multiplier);
if (is_swipe) {
if (!accurate_aim)
{
throw_stone(rot_matrix.MultiplyVector(transform.forward));
}
else
{
RaycastHit hit;
lake_collider.Raycast(e_ray, out hit, 1000);
r_end = hit.point;
throw_stone(e_ray.direction);
}
}
break;
}
#endif
}
private void throw_stone (Vector3 dir)
{
GameObject new_stone;
new_stone = (GameObject)Object.Instantiate(stone_prefab, transform.position + transform.forward * 2 - new Vector3(0, 1, 0), Quaternion.identity);
// print("Forward: " + transform.forward);
// Vector3 dir = rot_matrix.MultiplyVector(transform.forward);
new_stone.rigidbody.AddForce(dir * throw_strength * throw_multiplier);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment