Skip to content

Instantly share code, notes, and snippets.

@tudorelu
Created May 7, 2019 02:59
Show Gist options
  • Select an option

  • Save tudorelu/07a0209cc9f1e5dd8dd0e2b2e73aed62 to your computer and use it in GitHub Desktop.

Select an option

Save tudorelu/07a0209cc9f1e5dd8dd0e2b2e73aed62 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.XR.ARFoundation;
/// <summary>
/// Controls the basketball.
/// </summary>
[RequireComponent(typeof(Rigidbody))]
public class BallControl : MonoBehaviour
{
/// <summary>
/// The force with which the ball is thrown
/// </summary>
public float m_ThrowForce = 100f;
/// <summary>
/// The x axis multiplier for the throwing force
/// </summary>
public float m_ThrowDirectionMultiplierX = 0.17f;
/// <summary>
/// The y axis multiplier for the throwing force
/// </summary>
public float m_ThrowDirectionMultiplierY = 0.67f;
/// <summary>
/// Distace Between Ball and Camera
/// </summary>
public Vector3 m_BallCameraOffset = new Vector3(0f, -1.6f, 2);
/// <summary>
/// The following variables contain the state
/// of the current throw of the ball.
/// </summary>
private Vector3 startPos;
private Vector3 swishDirection;
private float startTime = 0.0f;
private float endTime = 0.0f;
private float swishDuration = 0.0f;
private bool directionChosen = false;
private bool clickStarted = false;
[SerializeField]
GameObject ARCam;
[SerializeField]
ARSessionOrigin m_SessionOrigin;
Rigidbody rb;
void Start()
{
rb = gameObject.GetComponent<Rigidbody>();
m_SessionOrigin = GameObject.Find("AR Session Origin").GetComponent<ARSessionOrigin>();
ARCam = m_SessionOrigin.transform.Find("AR Camera").gameObject;
ResetBallPosition();
}
void Update()
{
ThrowControls();
// reset ball position in front of camera 5 seconds after we've thrown it.
if (Time.time - endTime >= 5 && Time.time - endTime <= 6)
ResetBallPosition();
}
/// <summary>
/// Control the ball using mouse clicks.
/// </summary>
void ThrowControls()
{
// when we click / touch for the first time
if (Input.GetMouseButtonDown(0))
{
startPos = Input.mousePosition;
startTime = Time.time;
clickStarted = true;
Debug.Log("Click Started");
directionChosen = false;
}
// when we release the click / touch
else if (Input.GetMouseButtonUp(0))
{
endTime = Time.time;
swishDuration = endTime - startTime;
swishDirection = Input.mousePosition - startPos;
Debug.Log("Click Ended");
directionChosen = true;
}
// once we chose the direction, make ball follow that direction
if (directionChosen)
{
rb.mass = 1;
rb.useGravity = true;
rb.AddForce(
ARCam.transform.forward * m_ThrowForce / swishDuration +
ARCam.transform.up * swishDirection.y * m_ThrowDirectionMultiplierY +
ARCam.transform.right * swishDirection.x * m_ThrowDirectionMultiplierX);
clickStarted = false;
directionChosen = false;
startTime = 0.0f;
swishDuration = 0.0f;
startPos = new Vector3(0, 0, 0);
swishDirection = new Vector3(0, 0, 0);
}
}
/// <summary>
/// Reset ball's position
/// </summary>
void ResetBallPosition()
{
// change the rigidbody's properties such that the ball doesn't move until we throw it
rb.mass = 0;
rb.useGravity = false;
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
endTime = 0.0f;
// position ball right in front of AR Camera
Vector3 ballPos = ARCam.transform.position + ARCam.transform.forward + ARCam.transform.up * -0.45f;
transform.position = ballPos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment