State Machine C# - c#

I am doing a project in Unity with C# and I want to have two drones that will wander in the room, and then attack each other using a state machine.
I have three classes AttackState, ChaseState and WonderState
For example, this is my WanderState Class:
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Linq;
using System;
using UnityEngine;
public class WanderState : BaseState
{
private Vector3? _destination;
private float stopDistance = 1.5f;
private float turnSpeed = 1f;
private readonly LayerMask _layerMask = LayerMask.NameToLayer("Walls");
private float _rayDistance = 5.0f;
private Quaternion _desiredRotation;
private Vector3 _direction;
private Drone _drone;
public WanderState(Drone drone) : base(drone.gameObject)
{
_drone = drone;
}
public override Type Tick()
{
var chaseTarget = CheckForAggro();
if (chaseTarget != null)
{
_drone.SetTarget(chaseTarget);
return typeof(ChaseState);
}
if (_destination.HasValue == false || Vector3.Distance(transform.position, _destination.Value) <= stopDistance)
{
FindRandomDestination();
}
transform.rotation = Quaternion.Slerp(transform.rotation, _desiredRotation, Time.deltaTime * turnSpeed); //Time.deltaTime * turnSpeed
if (IsForwardBlocked()) //IsForwardBlocked()
{
transform.rotation = Quaternion.Lerp(transform.rotation, _desiredRotation, 0.2f);
}
else
{
float droneSpeed = 2f;
transform.Translate(Vector3.forward * Time.deltaTime * droneSpeed);
}
Debug.DrawRay(transform.position, _direction * _rayDistance, Color.red);
while (IsPathBlocked())
{
FindRandomDestination();
Debug.Log("Wall");
}
return null;
}
private bool IsForwardBlocked()
{
Ray ray = new Ray(transform.position, transform.forward);
return Physics.SphereCast(ray, 0.5f, _rayDistance, _layerMask);
}
private bool IsPathBlocked()
{
Rigidbody obj = new Rigidbody();
Ray ray = new Ray(transform.position, _direction);
return Physics.SphereCast(ray, 0.5f, _rayDistance, _layerMask);
}
private void FindRandomDestination()
{
Vector3 testPosition = (transform.position + (transform.forward * 4f)) + new Vector3(UnityEngine.Random.Range(-4.5f, 4.5f), 0f,UnityEngine.Random.Range(-4.5f, 4.5f));
_destination = new Vector3(testPosition.x, 1f, testPosition.z);
_direction = Vector3.Normalize(_destination.Value - transform.position);
_direction = new Vector3(_direction.x, 0f, _direction.z);
_desiredRotation = Quaternion.LookRotation(_direction);
Debug.Log("Got direction");
}
Quaternion startingAngle = Quaternion.AngleAxis(-60, Vector3.up);
Quaternion stepAngle = Quaternion.AngleAxis(5, Vector3.up);
private Transform CheckForAggro()
{
float aggroRadius = 5f;
RaycastHit hit;
var angle = transform.rotation * startingAngle;
var direction = angle * Vector3.forward;
var pos = transform.position;
for (var i = 0; i < 24; i++)
{
if (Physics.Raycast(pos, direction, out hit, aggroRadius))
{
var drone = hit.collider.GetComponent<Drone>();
if (drone != null && drone.Team1 != gameObject.GetComponent<Drone>().Team1)
{
Debug.DrawRay(pos, direction * hit.distance, Color.red);
return drone.transform;
}
else
{
Debug.DrawRay(pos, direction * hit.distance, Color.yellow);
}
}
else
{
Debug.DrawRay(pos, direction * aggroRadius, Color.white);
}
direction = stepAngle * direction;
}
return null;
}
}
Then I have 2 drones, and each of them have a Drone script and a StateMachine script as follows:
public class Drone : MonoBehaviour
{
[SerializeField] private Team _team;
[SerializeField] private GameObject _laserVisual;
public Transform Target { get; private set; }
public Team Team1=> _team;
public StateMachine StateMachine => GetComponent<StateMachine>();
private void Awake()
{
InitializeStateMachine();
}
private void InitializeStateMachine()
{
var states = new Dictionary<Type, BaseState>()
{
{typeof(WanderState), new WanderState(this) },
{typeof(ChaseState), new ChaseState(this) },
{typeof(AttackState), new AttackState(this) }
};
GetComponent<StateMachine>().SetStates(states);
}
public void SetTarget(Transform target)
{
Target = Target;
}
public void FireWeapon()
{
_laserVisual.transform.position = (Target.position + transform.position) / 2f;
float distance = Vector3.Distance(a: Target.position, b: transform.position);
_laserVisual.transform.localScale = new Vector3(0.1f, 0.1f, distance);
_laserVisual.SetActive(true);
StartCoroutine(TurnOffLaser());
}
public IEnumerator TurnOffLaser()
{
yield return new WaitForSeconds(0.25f);
_laserVisual.SetActive(false);
if (Target != null)
{
GameObject.Destroy(Target.gameObject);
}
}
public enum Team
{
Red,
Blue
}
public class StateMachine : MonoBehaviour
{
private Dictionary<Type, BaseState> _availableStates;
public BaseState CurrentState { get; private set; }
public event Action<BaseState> OnStateChanged;
public void SetStates(Dictionary<Type, BaseState> states)
{
_availableStates = states;
}
private void Update()
{
if(CurrentState == null)
{
CurrentState = _availableStates.Values.First();
}
var nextState = CurrentState?.Tick();
if (nextState != null && nextState != CurrentState.GetType())
{
SwitchToNewState(nextState);
}
}
public void SwitchToNewState(Type nextState)
{
CurrentState = _availableStates[nextState];
OnStateChanged?.Invoke(CurrentState);
}
}
The problem I am facing is that my drones are going through the room walls
I tried to set for the walls a Mesh Collider or a Box collider, but none of these options worked. Also, for the drones I have a sphere collider.
Does anyone know why this behaviour and what can I do to fix it?

Add a rigidbody to the drone and make sure isKinematic is unchecked.

Related

How to check if object is inside terrain area?

I have a plane and when i click on the plane it's spawning objects on the plane area and on the terrain ground.
but i want to do that if the plane or part of it is out of the terrain area that it will disable the spawning part.
This screenshot show the plane above the terrain.
and this screenshot where the plane is out of the terrain area but i can still click on the plane and spawn objects.
This is the spawn objects script :
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class SpawnObjects : MonoBehaviour
{
public GameObject prefabToSpawn;
public Terrain terrain;
public CustomPlane plane;
public GameObject spawnedTerrainObjectsParent;
public GameObject spawnedPlaneObjectsParent;
public bool go = false;
public int numberOfObjects;
public float duration;
public float yOffset = 0.5f;
public bool isParent = true;
private float terrainWidth;
private float terrainLength;
private float xTerrainPos;
private float zTerrainPos;
private float planeWidth;
private float planeLength;
private float xPlanePos;
private float zPlanePos;
private float yValTerrain;
private float yValPlane;
private float randXTerrain;
private float randZTerrain;
private float randXPlane;
private float randZPlane;
private GameObject cube;
private Collider terrainCollider;
void Awake()
{
if (terrain != null)
{
//Get terrain size
terrainWidth = terrain.terrainData.size.x;
terrainLength = terrain.terrainData.size.z;
//Get terrain position
xTerrainPos = terrain.transform.position.x;
zTerrainPos = terrain.transform.position.z;
terrainCollider = terrain.GetComponent<Collider>();
}
if (plane != null)
{
planeWidth = plane.width;
planeLength = plane.length;
xPlanePos = plane.transform.position.x;
zPlanePos = plane.transform.position.z;
}
if (isParent)
{
if(spawnedTerrainObjectsParent == null)
{
spawnedTerrainObjectsParent = GameObject.Find("Spawned Terrain Parent");
}
if(spawnedPlaneObjectsParent == null)
{
spawnedPlaneObjectsParent = GameObject.Find("Spawned Plane Parent");
}
}
Camera.main.transform.position =
new Vector3(plane.GetComponent<Renderer>().bounds.center.x, 635,
plane.GetComponent<Renderer>().bounds.center.z);
if (go)
{
StartCoroutine(Generate());
}
}
IEnumerator Generate()
{
//Generate the Prefab on the generated position
for (int i = 0; i < numberOfObjects; i++)
{
if (terrain != null)
{
//Generate random x,z,y position on the terrain
randXTerrain = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
randZTerrain = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);
yValTerrain = Terrain.activeTerrain.SampleHeight(new Vector3(randXTerrain, 0, randZTerrain));
yValTerrain = yValTerrain + yOffset;
GameObject objInstanceTerrain = (GameObject)Instantiate(prefabToSpawn,
new Vector3(randXTerrain, yValTerrain, randZTerrain), Quaternion.identity);
objInstanceTerrain.name = "Terrain Spawned";
if (isParent)
{
objInstanceTerrain.transform.parent = spawnedTerrainObjectsParent.transform;
}
}
if (plane != null)
{
randXPlane = UnityEngine.Random.Range(xPlanePos, xPlanePos + planeWidth);
randZPlane = UnityEngine.Random.Range(zPlanePos, zPlanePos + planeLength);
yValPlane = 0;
yValPlane = yValPlane + yOffset;
GameObject objInstancePlane = (GameObject)Instantiate(prefabToSpawn,
new Vector3(randXPlane, yValPlane, randZPlane), Quaternion.identity);
objInstancePlane.name = "Plane Spawned";
if (isParent)
{
objInstancePlane.transform.parent = spawnedPlaneObjectsParent.transform;
}
}
if (duration > 0)
{
yield return new WaitForSeconds(duration);
}
}
}
private void Update()
{
SpawnThroughPlane();
}
public void SpawnThroughPlane()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.GetComponent<MeshCollider>().Raycast(ray, out hit, Mathf.Infinity))
{
cube = Instantiate(prefabToSpawn);
cube.transform.position = hit.point;
float heightFromTerrain = Terrain.activeTerrain.SampleHeight(hit.point);
var posy = hit.point.y;
posy -= heightFromTerrain;
var newpos = new Vector3(hit.point.x, hit.point.y - posy, hit.point.z);
Instantiate(prefabToSpawn, newpos, Quaternion.identity);
}
}
}
}

Values Getting Skewed When Passed Between Scripts Unity

I'm working on a portal system and am up to making the traveller go through the portal. For this, I just set the traveller's position to the position of the other portal. When I do this, however, the Z rotation of the camera gets skewed, and they are teleported slightly higher than they should. Any help is welcome <3
Portal Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Portal : MonoBehaviour
{
//Portal Camera Positioning
public Portal linkedPortal;
private Camera portalCam;
private Camera playerCam;
//Portal Sceen Texture
private PortalScreen portalScreen;
private MeshRenderer screenRen;
private RenderTexture viewTexture;
//Travellers
//[SerializableField]
List<PortalTraveller> trackedTravellers;
public Vector3 desiredCameraRotation;
public Vector3 desiredCameraPosition;
private Transform globalRuler;
PortalTraveller[] travellersToAdd;
[HideInInspector]
public float travellerDistance;
//Debug
[HideInInspector]
public float dstToPlayer;
void Awake()
{
playerCam = Camera.main;
portalCam = linkedPortal.GetComponentInChildren<Camera>();
portalScreen = GetComponentInChildren<PortalScreen>();
screenRen = portalScreen.GetComponent<MeshRenderer>();
globalRuler = GameObject.FindWithTag("GlobalRuler").transform;
trackedTravellers = new List<PortalTraveller>();
travellersToAdd = GameObject.FindObjectsOfType<PortalTraveller>();
foreach (PortalTraveller traveller in travellersToAdd)
{
if (!trackedTravellers.Contains(traveller))
{
trackedTravellers.Add(traveller);
}
}
}
void Update()
{
Render();
}
static bool VisibleFromCamera(Renderer renderer, Camera camera)
{
Plane[] frustumPlanes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(frustumPlanes, renderer.bounds);
}
void Render()
{
if (!VisibleFromCamera(screenRen, playerCam))
{
return;
}
desiredCameraPosition = transform.position - (playerCam.transform.position - linkedPortal.portalScreen.screenPos);
desiredCameraPosition.y = playerCam.transform.position.y;
desiredCameraRotation = playerCam.transform.eulerAngles + new Vector3 (0f, 180f, 0f);
portalCam.transform.position = desiredCameraPosition;
portalCam.transform.eulerAngles = desiredCameraRotation;
if (viewTexture == null || viewTexture.width != Screen.width || viewTexture.height != Screen.height)
{
if (viewTexture != null)
{
viewTexture.Release();
}
viewTexture = new RenderTexture (Screen.width, Screen.height, 0);
portalCam.targetTexture = viewTexture;
screenRen.material.SetTexture("_MainTex", viewTexture);
}
}
void LateUpdate()
{
HandleTravellers();
}
void HandleTravellers()
{
for (int i = 0; i < trackedTravellers.Count; i++)
{
PortalTraveller traveller = trackedTravellers[i];
Transform travellerT = trackedTravellers[i].transform;
Vector3 toTraveller = traveller.transform.position - transform.position;
int portalSide = System.Math.Sign(Vector3.Dot(toTraveller, transform.right));
int portalSideOld = System.Math.Sign(Vector3.Dot(traveller.previousTravellerVector, transform.right));
travellerDistance = Mathf.Abs(Vector3.Distance(transform.position, travellerT.position));
//Debug.Log("Handled");
if (travellerDistance > linkedPortal.travellerDistance)
{
break;
}
if (portalSide < portalSideOld && travellerDistance < globalRuler.transform.lossyScale.z / 2 )
{
//Debug.Log(travellerDistance);
var positionOld = travellerT.position;
var rotationOld = travellerT.rotation;
Quaternion rot = Quaternion.Euler(desiredCameraRotation.x, desiredCameraRotation.y, 0f);
traveller.Teleport(transform, linkedPortal.transform, desiredCamerxaPosition, rot);
traveller.previousTravellerVector = toTraveller;
//trackedTravellers.RemoveAt(i);
i--;
}
else
{
traveller.previousTravellerVector = toTraveller;
dstToPlayer = Mathf.Abs(Vector3.Distance(transform.position, travellerT.position));
}
}
}
void OnValidate()
{
if (linkedPortal != null)
{
linkedPortal.linkedPortal = this;
}
travellersToAdd = GameObject.FindObjectsOfType<PortalTraveller>();
}
}
Traveller Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PortalTraveller : MonoBehaviour
{
public Vector3 previousTravellerVector;
public void Teleport(Transform originalPortal, Transform linkedPortal, Vector3 pos, Quaternion rot)
{
transform.position = pos;
transform.rotation = rot;
}
}

Why the transform is not moving to the new radius when changing the radius at runtime?

if dc is not null i'm using a radius from another script and it's working fine when i'm changing the radius value in the DrawCircle script the transform in RotateAroundTarget is moving smooth to the new radius.
but if dc variable is null i want to use the local radius variable for setting the radius in runtime but when changing the radius the transform is not moving to the new radius.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateAroundTarget : MonoBehaviour
{
public Transform target;
public float rotatingSpeed;
public float movingSpeed;
public Vector3 axis;
public bool randomHeight;
public float setRandomHeight = 1;
public float radius;
public DrawCircle dc;
private float lastRadius;
private bool move = false;
private float t = 0.0f;
public float upperLimit, lowerLimit, delay;
private float prevHeight;
private Vector3 radiusPosition;
private void Start()
{
if (dc != null)
{
lastRadius = dc.xRadius;
}
else
{
lastRadius = radius;
}
move = true;
}
private void Update()
{
if (dc != null)
{
radiusPosition = new Vector3(target.position.x, target.position.y, dc.xRadius);
}
else
{
radiusPosition = new Vector3(target.position.x, target.position.y, radius);
}
if (move == false)
{
transform.RotateAround(target.position, axis, rotatingSpeed * Time.deltaTime);
t += Time.deltaTime;
if (t > delay)
{
prevHeight = setRandomHeight;
setRandomHeight = Random.Range(lowerLimit, upperLimit);
t = 0;
}
var tt = transform.position;
tt.y = Mathf.Lerp(prevHeight, setRandomHeight, t);
transform.position = tt;
}
if (dc != null)
{
if (lastRadius != dc.xRadius)
{
move = true;
lastRadius = dc.xRadius;
}
}
else
{
if (lastRadius != radius)
{
move = true;
lastRadius = radius;
}
}
if (move)
{
if (transform.position != radiusPosition)
{
float step = movingSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position,
radiusPosition, step);
}
else
{
move = false;
}
}
}
}

Unity 2D, control a simple shot

I have to control a shot of a ball with touch. All works fine but I need to start my trajectory when I touch the screen, but the trajectory start only when the ball is touched. Is because the script is attached to the ball?
All touch input except touches on the ball is ignored.
Here is the C# script , can someone help me?
using UnityEngine;
using System.Collections;
using System;
namespace Shorka.BallTrajectory
{
public enum PullState
{
Idle,
UserPulling,
ObjFlying
}
public class PullCtrl : MonoBehaviour
{
#region fields
//[SerializeField] private Transform throwTarget;
[SerializeField] private ThrownObject throwObj;
[SerializeField] private Transform dotHelper;
[SerializeField] private Transform pullingStartPoint;
[Space(5)]
[Tooltip("this linerenderer will draw the projected trajectory of the thrown object")]
[SerializeField]
private LineRenderer trajectoryLineRen;
[SerializeField]
private TrailMaker trail;
[Space(5)]
[SerializeField]
private float throwSpeed = 10F;
[Tooltip("Max Distance between 'PullingStartPoint' and pulling touch point")]
[SerializeField]
private float maxDistance = 1.5F;
[SerializeField]
private float coofDotHelper = 1.5F;
[Tooltip("Related to length of trajectory line")]
[SerializeField]
private int qtyOfsegments = 13;
[Tooltip("Step of changing trajectory dots offset in runtime")]
[SerializeField]
private float stepMatOffset = 0.01F;
[Tooltip("Z position of trajectory dots")]
[SerializeField]
private float dotPosZ = 0F;
private PullState pullState;
private Camera camMain;
//private Collider2D collThrowTarget;
private Rigidbody2D rgThrowTarget;
private Vector3 posPullingStart;
private Vector3 initPos;
private TrajectoryCtrl trajCtrl;
#endregion
public Vector3 PosDotHelper { get { return dotHelper.position; } }
public Vector3 PosThrowTarget { get { return throwObj.transform.position; } }
public int QtyOfsegments { get { return qtyOfsegments; } }
public float DotPosZ { get { return dotPosZ; } }
public Vector3 PosPullingStart { get { return posPullingStart; } }
public float StepMatOffset { get { return stepMatOffset; } }
void Awake()
{
trail.emit = false;
trajCtrl = new TrajectoryCtrl(this, trajectoryLineRen);
}
void Start()
{
camMain = Camera.main;
pullState = PullState.Idle;
posPullingStart = pullingStartPoint.position;
initPos = PosThrowTarget;
}
void Update()
{
SwitchStates();
}
private void SwitchStates()
{
switch (pullState)
{
case PullState.Idle:
if (Input.touchCount> 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
Debug.Log("Screen touched");
//get the point on screen user has tapped
Vector3 location = camMain.ScreenToWorldPoint(Input.GetTouch(0).position);
//if user has tapped onto the ball
if (throwObj.Collider == Physics2D.OverlapPoint(location))
pullState = PullState.UserPulling;
}
break;
case PullState.UserPulling:
dotHelper.gameObject.SetActive(true);
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
//get touch position
Vector3 touchPos = camMain.ScreenToWorldPoint(Input.GetTouch(0).position);
touchPos.z = 0;
//we will let the user pull the ball up to a maximum distance
if (Vector3.Distance(touchPos, posPullingStart) > maxDistance)
{
Vector3 maxPosition = (touchPos - posPullingStart).normalized * maxDistance + posPullingStart;
maxPosition.z = dotHelper.position.z;
dotHelper.position = maxPosition;
}
else
{
touchPos.z = dotHelper.position.z;
dotHelper.position = touchPos;
}
float distance = Vector3.Distance(posPullingStart, dotHelper.position);
trajCtrl.DisplayTrajectory(distance);
}
else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Canceled || Input.GetTouch(0).phase == TouchPhase.Ended)
{
float distance = Vector3.Distance(posPullingStart, dotHelper.position);
trajectoryLineRen.enabled = false;
ThrowObj(distance);
}
break;
default:
break;
}
}
//private Vector2 velocityToRg = Vector2.zero;
private void ThrowObj(float distance)
{
Debug.Log("ThrowObj");
pullState = PullState.Idle;
Vector3 velocity = posPullingStart - dotHelper.position;
//velocityToRg = CalcVelocity(velocity, distance);
throwObj.ThrowObj(CalcVelocity(velocity, distance));
//rgThrowTarget.velocity = velocityToRg;
//rgThrowTarget.isKinematic = false;
trail.enabled = true;
trail.emit = true;
dotHelper.gameObject.SetActive(false);
}
public void Restart(Vector3 posThrownObj)
{
trail.emit = false;
trail.Clear();
StartCoroutine(ClearTrail());
trajectoryLineRen.enabled = false;
dotHelper.gameObject.SetActive(false);
pullState = PullState.Idle;
throwObj.Reset(posThrownObj);
}
private readonly WaitForSeconds wtTimeBeforeClear = new WaitForSeconds(0.3F);
IEnumerator ClearTrail()
{
yield return wtTimeBeforeClear;
trail.Clear();
trail.enabled = false;
}
Vector3 velocity = Vector3.zero;
public Vector3 CalcVelocity(Vector3 diff, float distance)
{
velocity.x = diff.x * throwSpeed * distance * coofDotHelper;
velocity.y = diff.y * throwSpeed * distance * coofDotHelper;
return velocity;
}
}
}
Its quite easy. you need to add these two scripts to the gameobject (not the ball but the one which is used to shoot the ball something like a gun)
TouchEventTrigger.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class TouchEventTrigger : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler {
public TouchEvent onClick;
public TouchEvent onDown;
public TouchEvent onUp;
public void OnPointerClick(PointerEventData e) {
onClick.Invoke(e);
}
public void OnPointerDown(PointerEventData e) {
onDown.Invoke(e);
}
public void OnPointerUp(PointerEventData e) {
onUp.Invoke(e);
}
}
[System.Serializable]
public class TouchEvent : UnityEvent<PointerEventData> {}
Shooter.cs
public void TryBeginAim(PointerEventData e) {
if(canShoot) {
initialTouchPos = e.position;
}
}
public void TryAim(PointerEventData e) {
if(canShoot) {
direction = initialTouchPos - e.position;
float mag = (initialTouchPos - e.position).magnitude;
float scale = Mathf.Clamp(mag/maxMagnitude, scaleThreshold, 1f);
angle = Mathf.Atan2(direction.y,direction.x) * Mathf.Rad2Deg + 90f;
aim.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
aim.transform.localScale = Vector3.one * scale;
aim.SetActive(e.position.y < initialTouchPos.y);
}
}
public void TryShoot(PointerEventData e) {
if(canShoot && aim.activeInHierarchy) {
canShoot = false;
StartCoroutine(ShootRoutine(balls));
}
aim.SetActive(false);
}
private IEnumerator ShootRoutine(int b) {
// write code to start shooting balls.
}
Then connect the touch events with the methods like this:
1. OnPointerClick => TryBeginAim
2. OnPointerDown => TryAim
3. OnPointerUp => TryShoot
That's All !!
I hope this helps to solve this issue. Enjoy!

error CS0120: An object reference is required to access non-static member `CameraOperator.InvertMouseY(float)'

Hi I'm making a RTS style game and having trouble getting selecting and highlighting my vehicles. Here is the errors Im having. Any help would be appreciated.
Assets/Scripts/Unit2.cs(19,51): error CS0120: An object reference is required to access non-static member `CameraOperator.InvertMouseY(float)'
Here is the script.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Unit2 : MonoBehaviour
{
public bool selected = false;
public float floorOffset = 1;
public float speed = 5;
public float stopDistanceOffset = 0.5f;
private Vector3 moveToDest = Vector3.zero;
private void Update ()
{
if (renderer.isVisible && Input.GetMouseButtonDown (0))
{
Vector3 camPos = Camera.main.WorldToScreenPoint (transform.position);
camPos.y = CameraOperator.InvertMouseY(camPos.y); "This Line Error"
selected = CameraOperator.Selection.Contains (camPos);
if (selected)
{
renderer.material.color = Color.red;
}
else
{
renderer.material.color = Color.white;
}
if(selected && Input.GetMouseButtonUp(1))
{
Vector3 destination = CameraOperator.getDestination();
if(destination != Vector3.zero)
{
moveToDest = destination;
moveToDest.y += floorOffset;
}
}
}
UpdateMove();
}
private void UpdateMove()
{
if((moveToDest != Vector3.zero) && (transform.position != moveToDest))
{
Vector3 direction = (moveToDest - transform.position).normalized;
direction.y = 0;
transform.rigidbody.velocity = direction * speed;
if(Vector3.Distance(transform.position, moveToDest) < stopDistanceOffset)
{
moveToDest = Vector3.zero;
}
}
else
{
transform.rigidbody.velocity = Vector3.zero;
}
}
}
Here is the CameraOperator script.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CameraOperator : MonoBehaviour
{
public Texture2D selectionHighlight = null;
public static Rect Selection = new Rect (0, 0, 0, 0);
private Vector3 StartClick = -Vector3.one;
private static Vector3 moveToDestination = Vector3.zero;
private static List<string> passables = new List<string> () {"Floor"};
private void Update ()
{
CheckCamera ();
CleanUp ();
}
public void CheckCamera ()
{
if (Input.GetMouseButtonDown (0)) {
StartClick = Input.mousePosition;
}
if (Input.GetMouseButtonUp (0)) {
StartClick = -Vector3.one;
}
if (Input.GetMouseButton (0)) {
Selection = new Rect (StartClick.x, InvertMouseY (StartClick.y), Input.mousePosition.x - StartClick.x, InvertMouseY (Input.mousePosition.y) - InvertMouseY (StartClick.y));
if (Selection.width < 0) {
Selection.x += Selection.width;
Selection.width = -Selection.width;
}
if (Selection.height < 0) {
Selection.y += Selection.height;
Selection.height = -Selection.height;
}
}
}
public float InvertMouseY (float y)
{
return Screen.height - y;
}
private void CleanUp ()
{
if (!Input.GetMouseButtonUp (1)) {
moveToDestination = Vector3.zero;
}
}
public static Vector3 getDestination ()
{
RaycastHit hit;
Ray r = Camera.main.ScreenPointToRay (Input.mousePosition);
if (moveToDestination == Vector3.zero) {
if (Physics.Raycast (r, out hit)) {
while (!passables.Contains(hit.transform.gameObject.name)) {
if (!Physics.Raycast (hit.transform.position, r.direction, out hit))
break;
}
}
if (hit.transform != null) {
moveToDestination = hit.point;
}
}
return moveToDestination;
}
}
Your InvertMouseY function is not marked as static, but you're trying to call it as a static function, not an instance member. CameraOperator.InvertMouseY(camPos.y) is a static call (you're calling a function through the type name CameraOperator, not through an instance of CameraOperator).
Since your InvertMouseY function doesn't seem to depend on any kind of state (only its single input parameter), the simplest way to fix this problem is to add static to the function's declaration.
public static float InvertMouseY (float y)
{
return Screen.height - y;
}

Categories