Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //returns -1 when to the left, 1 to the right, and 0 for forward/backward
- public float AngleDir(Vector3 fwd, Vector3 targetDir, Vector3 up)
- {
- Vector3 perp = Vector3.Cross(fwd, targetDir);
- float dir = Vector3.Dot(perp, up);
- if (dir > 0.0f) {
- return 1.0f;
- } else if (dir < 0.0f) {
- return -1.0f;
- } else {
- return 0.0f;
- }
- }
- RaycastHit hit;
- // Does the ray intersect any objects excluding the player layer
- if(doSteering == true)
- {
- if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
- {
- Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
- Debug.Log("Did Hit");
- float dirHit = AngleDir(transform.forward, hit.transform.position, transform.up);
- if(dirHit == -1)
- {
- rbody.AddRelativeForce(Vector3.left * 50, ForceMode.Acceleration);
- Debug.LogError("FORCE FROM THE LEFT");
- }
- else if(dirHit == 1)
- {
- rbody.AddRelativeForce(Vector3.right * 50, ForceMode.Acceleration);
- Debug.LogError("FORCE FROM THE RIGHT");
- }
- else
- {
- //front;
- rbody.AddRelativeForce(Vector3.up*50, ForceMode.Acceleration);
- Debug.LogError("FORCE IT UP");
- }
- }
- else
- {
- Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement