Hi. I’m pretty new to Unity but I am not new to coding and scripting and such. However, I am trying to create what could be considered a Fly simulation, as in the insect. A first-person fly that has the ability to fly around and interact with objects but also land on the nearest surface, whatever that surface might be, and walk around on it as if you were using the FPS controller. I have some of a script written already but I could use suggestions and help on how to get a couple of things working because at the moment i really don’t know how to go about it. Such as:
– A fly can land on any surface, no matter how it is oriented.
– I want the fly to be able to walk over the edge of surfaces (for example over the edge of a table).
–I want to be able to translate between two control systems, a FPS control system for on surfaces and a flying control system based on force vectors.
I will post what I have so far but it is obviously subject to change. Also I don’t care if the help is given in Javascript or C#, though because I want to use mostly C# that would be preferred. However, I can just as easily translate JS to C#. If you have any further questions I will be happy to answer them. Thanks a lot.
using UnityEngine;
using System.Collections;
/******************************
*Class to find the nearest landing surface for the main character and forcing
*a placement on that nearest surface if the capslock is pressed within the threshold.
******************************/
public class FindNearestLandSurface : MonoBehaviour
{
public RaycastHit nearestSurface;
public float landingThreshold = 15.0F;
//public Transform flyChar = Camera.main.transform;
//Setting reference vectors for nonstandard directions
private Vector3 left = -Vector3.right;
private Vector3 down = -Vector3.up;
//private Vector3 back = -Vector3.forward;
/**********************************
*Setting easily referenced world vectors based on the reference vectors
*and position of main character.
**********************************/
private Vector3 fwdD = transform.TransformDirection (Vector3.forward);
private Vector3 rightD = transform.TransformDirection (Vector3.right);
private Vector3 leftD = transform.TransformDirection (left);
private Vector3 upD = transform.TransformDirection (Vector3.up);
private Vector3 downD = transform.TransformDirection (down);
//private Vector3 bkwdD = transform.TransformDirection (back);
/*********************************
*Finds the closest surface to the main character.
**Returns: RaycastHit object corresponding to the nearest surface
***Known Issues: Assumes enclosed space, there is no handling for infinite Raycasts.
*********************************/
RaycastHit FindMinRay ()
{
RaycastHit[] allHits;
Physics.Raycast(transform.position,fwdD, out allHits[0]);
Physics.Raycast(transform.position,rightD, out allHits[1]);
Physics.Raycast(transform.position,leftD, out allHits[2]);
Physics.Raycast(transform.position,upD, out allHits[3]);
Physics.Raycast(transform.position, downD, out allHits[4]);
RaycastHit temp = allHits[0];
for (int i = 1; i <= 4; i++)
{
if(allHits[i].distance < temp.distance)
{
temp = allHits[i];
}
}
return temp;
}
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Event.capsLock == false nearestSurface.distance > landingThreshold)
{
nearestSurface = FindMinRay();
}
else if(nearestSurface.distance < landingThreshold Event.capsLock == true)
{
transform.position = nearestSurface.point;
}
else if () {
}
}
}