Skip to content

Instantly share code, notes, and snippets.

@jjhesk
Created February 10, 2016 17:10
Show Gist options
  • Save jjhesk/88fdacf90e920d11713a to your computer and use it in GitHub Desktop.
Save jjhesk/88fdacf90e920d11713a to your computer and use it in GitHub Desktop.
reflection problem issue
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[RequireComponent (typeof(LineRenderer))]
public class RaycastReflectionGameCom : MonoBehaviour
{
//this game object's Transform
private Transform goTransform;
//the attached line renderer
private LineRenderer lineRenderer;
//a ray
private Ray ray;
//a RaycastHit variable, to gather informartion about the ray's collision
private RaycastHit hit;
//reflection direction
private Vector3 inDirection;
public float total_ray_length = 20f;
//the number of reflections
private int nReflections = 5;
//the number of points at the line renderer
private int nPoints;
//private bool enabled;
//private bool current_state = false;
private enum State
{
USER_CONTROL,
PROCESS_CONTROL}
;
private State current_state;
private List<Hashtable> ha = new List<Hashtable> ();
public List<Vector3> waypoints = new List<Vector3> ();
public Transform[] paths;
public Vector3[] lines;
public RaycastReflectionGameCom instance;
private float speed;
private GamePlay gameplay_controller;
//only draw on the controller interface
void OnDrawGizmos ()
{
try {
// iTween.DrawLineGizmos(paths, Color.yellow);
iTween.DrawPathGizmos (paths, Color.yellow);
iTween.DrawLine (paths, Color.red);
} catch {
}
}
public RaycastReflectionGameCom ()
{
//this is the constructor
// instance = new RaycastReflectionGameCom();
}
public void setFocus (Player player)
{
//nPoints = 1;
goTransform = player.get_transform ();
current_state = State.USER_CONTROL;
}
private void addRay (int n, Vector3 v3, Vector3 direction)
{
if (n < paths.Length) {
//just to get the waypoint information
GameObject cal = new GameObject ();
cal.layer = 8;
Transform trans = cal.transform;
trans.position = v3;
paths [n] = trans;
waypoints.Add (v3);
//lines[n] = trans.position;
}
}
private Transform moving_go;
private Vector3 floorPosition;
private float rayLength = 5;
private float lookAheadAmount = .0001f;
private float ySpeed = 0;
private float pathPosition = 0;
/**
* starting the game to reset all the waypoints
*/
public void Awake ()
{
//get the attached Transform component
//goTransform = this.GetComponent<Transform> ();
//get the attached LineRenderer component
lineRenderer = this.GetComponent<LineRenderer> ();
//waypoint = this.GetComponent<PathMaker>().linePoints;
//waypoints = new Vector3[nReflections + 1];
waypoints = new List<Vector3> ();
waypoints.Clear ();
//paths = new List<Transform> ();
paths = new Transform[nReflections + 1];
current_state = State.USER_CONTROL;
speed = 0;
}
private void makeReflection (Ray r, int index)
{
//float curDistance = Vector3.Distance (previous_hit_point, hit.point);
// if ((t - curDistance) > 0) {
if (Physics.Raycast (r.origin, r.direction, out hit, 100)) {
//cast the ray 100 units at the specified direction
//the reflection direction is the reflection of the current ray direction flipped at the hit normal
inDirection = Vector3.Reflect (index == 0 ? r.direction : inDirection, hit.normal);
//cast the reflected ray, using the hit point as the origin and the reflected direction as the direction
ray = new Ray (hit.point, inDirection);
//Draw the normal - can only be seen at the Scene tab, for debugging purposes
Debug.DrawRay (hit.point, hit.normal * 3, Color.blue);
//represent the ray using a line that can only be viewed at the scene tab
Debug.DrawRay (hit.point, inDirection * 100, Color.magenta);
//Print the name of the object the cast ray has hit, at the console
//if the number of reflections is set to 1c
if (nReflections == 1) {
//add a new vertex to the line renderer
//lineRenderer.SetVertexCount (++nPoints);
}
//set the position of the next vertex at the line renderer to be the same as the hit point
//lineRenderer.SetPosition (index + 1, hit.point);
if (index == 0) {
addRay (0, goTransform.position, inDirection);
}
//else {
addRay (index + 1, hit.point, inDirection);
//}
// index++;
//previous_hit_point = hit.point;
//tempp++;
}
//} else {
//}
}
private void SpeedControl ()
{
speed -= 0.002f; //fraction
if (speed > 0f && speed < 0.00001f) {
speed = 0f;
current_state = State.USER_CONTROL;
gameplay_controller.NotifyBallMovementDone ();
}
}
private void SimulateReflection ()
{
nReflections = Mathf.Clamp (nReflections, 1, nReflections);
ray = new Ray (goTransform.position, -1 * goTransform.forward);
Debug.DrawRay (goTransform.position, -1 * goTransform.forward * 100, Color.green);
for (int i=0; i<=nReflections; i++) {
makeReflection (ray, i);
}
}
public int currentWaypoint = 0;
private Player focus_player;
//called from another object
//moves when the mouse or touch is released
public void StartMovement (Player game_player_object)
{
focus_player = game_player_object;
GameObject go = game_player_object.goj () as GameObject;
game_player_object.Kinematic (true).Gravity (false);
gameplay_controller = game_player_object.get_center () as GamePlay;
//iTween.MoveTo (go, iTweenSettings ());
//iTween.PutOnPath (go, paths, 0f);
//iTween.MoveTo (go, iTweenSettings ());
//iTween.Init(go);
//iTween.Instantiate();
//iTween.Hash(iTweenSettings ());
//iTween.PutOnPath (go, paths, 0f);
moving_go = go.transform;
pathPosition = currentWaypoint = 0;
speed = 10;
current_state = State.PROCESS_CONTROL;
MoveToWaypoint ();
}
// not in use for now
private Hashtable iTweenSettings ()
{
//try to take of this one
//http://searchcode.com/codesearch/view/42546647
Hashtable args = new Hashtable ();
//设置路径的点
args.Add ("path", paths);
args.Add ("looktime", 1.0f);
args.Add ("lookahead", 0.1f);
//设置类型为线性,线性效果会好一些。
args.Add ("easeType", iTween.EaseType.linear);
//设置寻路的速度
args.Add ("speed", 50f);
//是否先从原始位置走到路径中第一个点的位置
args.Add ("movetopath", false);
//是否让模型始终面朝当面目标的方向,拐弯的地方会自动旋转模型
//如果你发现你的模型在寻路的时候始终都是一个方向那么一定要打开这个
args.Add ("looptype", iTween.LoopType.none);
//args.Add ("orienttopath", false);
return args;
}
private void FindFloorAndRotation ()
{
pathPosition += Time.deltaTime * speed;
float pathPercent = pathPosition % 1;
Vector3 coordinateOnPath = iTween.PointOnPath (paths, pathPercent);
Vector3 lookTarget;
//calculate look data if we aren't going to be looking beyond the extents of the path:
if (pathPercent - lookAheadAmount >= 0 && pathPercent + lookAheadAmount <= 1) {
//lookTarget = iTween.PointOnPath (paths, pathPercent + lookAheadAmount);
//look:
//moving_go.transform.LookAt (lookTarget);
//nullify all rotations but y since we just want to look where we are going:
float yRot = moving_go.eulerAngles.y;
moving_go.eulerAngles = new Vector3 (0, yRot, 0);
}
if (Physics.Raycast (coordinateOnPath, -Vector3.up, out hit, rayLength)) {
Debug.DrawRay (coordinateOnPath, -Vector3.up * hit.distance, Color.magenta);
floorPosition = hit.point;
}
}
private void MoveGameCharacter ()
{
//apply gravity:
moving_go.position = new Vector3 (floorPosition.x, moving_go.position.y - ySpeed, floorPosition.z);
//floor checking:
if (moving_go.position.y < floorPosition.y) {
ySpeed = 0;
moving_go.position = new Vector3 (floorPosition.x, floorPosition.y, floorPosition.z);
}
}
private void CompleteWayPoint ()
{
speed = 0f;
current_state = State.USER_CONTROL;
gameplay_controller.NotifyBallMovementDone ();
currentWaypoint = 0;
}
public iTween.EaseType easeType = iTween.EaseType.spring;
public void MoveToWaypoint ()
{
if (current_state == State.USER_CONTROL) return;
//Time = Distance / Rate:
float travelTime = Vector3.Distance (focus_player.get_position (), paths [currentWaypoint].position) / speed;
// Debug.Log (travelTime);
GameObject go = focus_player.goj () as GameObject;
//iTween:
iTween.MoveTo (go, iTween.Hash (
"position", paths [currentWaypoint],
//"time", travelTime,
"speed", speed,
"easytype", iTween.EaseType.spring,
"oncomplete", "MoveToWaypoint",
"oncompletetarget", this.gameObject,
"onupdate", "Moveupdate",
"onupdatetarget",this.gameObject,
"Looktarget", paths [currentWaypoint].position,
"looktime", .4f));
//Move to next waypoint:
currentWaypoint++;
//Debug.Log (currentWaypoint);
if (currentWaypoint > paths.Length - 2) {
CompleteWayPoint();
}
}
public void Moveupdate(){
Debug.Log(speed);
}
public void Update ()
{
if (current_state == State.USER_CONTROL) {
if (goTransform == null || lineRenderer == null)
return;
SimulateReflection ();
}else{
}
}
// public void Update ()
// {
// if (current_state == State.USER_CONTROL) {
// if (goTransform == null || lineRenderer == null)
// return;
// SimulateReflection ();
// } else {
// if (paths.Length > 1 && paths != null) {
// SpeedControl ();
// FindFloorAndRotation ();
// MoveGameCharacter ();
// }
// }
//
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment