Script created in namespace has limited access to everything
Hey guys, I am using the UnitySteer package, and it has a set of steering behaviours contained in a folder/namespace. I want to add one of my own steering behaviours in this folder. Problem is, that any script created in this has limited access to everything. Heck, even MonoBehaviour 'does not exist in this context'! Due to this I am not able to derive from another class in the namespace which I so much need! The only way now is to edit one of the existing behaviours/scripts!
What am I doing wrong? Is there anything I have to do to gain access to namespaces?I just used the same format as all the other scripts were having.
Here is the link to the package (https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjmtpD_x-vMAhVFOI8KHVgkDMgQFggcMAA&url=https%3A%2F%2Fgithub.com%2Fricardojmendez%2FUnitySteer&usg=AFQjCNH52VI_VPHwuemKoIJBWuWvlM-gpw&sig2=pG1rIahefmhaI2D-xmwOzw)
Could you share the important part of the code? (Where you want to add something)
Thanks for the reply Oribow. Fortunately(or unfortunately,) all it took was 2 restarts for that problem to be solved. However, I'll post the code in my answer.
Answer by varunvp · May 22, 2016 at 09:14 AM
It took 2(!) restarts, each 6 hours apart, to get it working. Don't know what happens to MonoDevelop sometimes...
Here is the code.
using UnityEngine;
namespace UnitySteer.Behaviors
{
[AddComponentMenu("UnitySteer/Steer/... for Obstacles")]
public class SteerForObstacles : Steering
{
public LayerMask m_LayerMask;
// Bounding sphere radius
public float m_BoundingSphereRadius = 1;
// Obstacle max distance
public float m_ObstacleMaxDistance = 10;
// Steering force conservation after avoiding
[Range(0.1f, 0.9f)]
public float m_SteeringForceConservation = 0.9f;
// Steering force conservation duration
public float m_SteeringForceConservationDuration = 1;
// Max floor angle
[Range(0, 90)]
public float m_MaxFloorAngle = 45;
// Steering force conservation timer
private float m_SteeringForceConservationTimer = 0;
// Old valid steering force
private Vector3 m_OldValidSteeringForce = Vector3.zero;
// Desired velocity
private Vector3 m_DesiredVelocity = Vector3.zero;
protected override Vector3 CalculateForce()
{
Vector3 SteeringForce;
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hitInfo;
Vector3 avoidanceForce = Vector3.zero;
// Calculate avoidance force
if (Physics.SphereCast(ray, m_BoundingSphereRadius, out hitInfo, m_ObstacleMaxDistance, m_LayerMask))
{
if (Vector3.Angle(hitInfo.normal, transform.up) > m_MaxFloorAngle)
{
avoidanceForce = Vector3.Reflect(Vehicle.Velocity, hitInfo.normal);
if (Vector3.Dot(avoidanceForce, Vehicle.Velocity) < -0.9f)
{
avoidanceForce = transform.right;
}
}
}
if (avoidanceForce != Vector3.zero)
{
// Calculate desired velocity
// m_DesiredVelocity = (avoidanceForce).normalized * Vehicle.MaxSpeed;
m_DesiredVelocity = (avoidanceForce);
// Calculate steering force
SteeringForce = m_DesiredVelocity - Vehicle.Velocity;
m_OldValidSteeringForce = SteeringForce;
m_SteeringForceConservationTimer = 0;
}
else
{
SteeringForce = Vector3.zero;
SteeringForce = SteeringForce * m_SteeringForceConservation;
m_SteeringForceConservationTimer += Time.deltaTime;
if (m_SteeringForceConservationTimer > m_SteeringForceConservationDuration)
{
m_SteeringForceConservationTimer = m_SteeringForceConservationDuration;
}
float ratio = 1 - (m_SteeringForceConservationTimer / m_SteeringForceConservationDuration);
SteeringForce = m_OldValidSteeringForce * ratio;
}
return SteeringForce;
}
void OnDrawGizmos()
{
Gizmos.color = Color.magenta;
Gizmos.DrawWireSphere(transform.position, m_BoundingSphereRadius);
Gizmos.DrawLine(transform.position, transform.position + transform.forward * m_ObstacleMaxDistance);
if (Vehicle == null)
{
return;
}
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, transform.position + m_DesiredVelocity);
//if (SteeringCore.Rigidbody != null)
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, transform.position + Vehicle.Velocity);
}
}
}
}