So I have just started using Unity a couple weeks ago and I have been making a custom script to move units to the location where I click on the screen. I don’t see any errors in the code and when I run the game, Unity says: “MissingMethodException: Method not found: ‘UnityEngine.Plane.Raycast’.”
This doesn’t make sense to me because this method is shown in it’s documentation. Below is my basic code for the character controller. I appreciate your time in helping me solve this.
var currentSpeed;
var walkSpeed = 3.0f;
var idleSpeed = 0.0f;
var selected = false;
static var selectedUnits = [];
private var targetPosition : Vector3;
function Start() {
targetPosition = transform.position;
}
function Update () {
var playerPlane;
var ray;
var hitDist = 0.0;
if(Input.GetKeyDown(KeyCode.Mouse0)) // left click
{
playerPlane = new Plane(Vector3.up, transform.position);
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var clickedPosition;
if (playerPlane.Raycast(ray, hitDist)) {
clickedPosition = ray.GetPoint(hitDist);
if(clickedPosition == transform.position)
{
SelectUnit(true);
}
}
}
if(Input.GetKeyDown(KeyCode.Mouse1)) // right click
{
playerPlane = new Plane(Vector3.up, transform.position);
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (playerPlane.Raycast(ray, hitDist)) {
targetPosition = ray.GetPoint(hitDist);
var targetRotation = Quaternion.LookRotation(targetPosition - transform.position);
transform.rotation = targetRotation;
currentSpeed = walkSpeed;
}
}
if(Vector3.Distance(transform.position, targetPosition) > .1)
{
transform.position += transform.forward * (currentSpeed * Time.deltaTime);
}
else
{
transform.position = targetPosition;
currentSpeed = idleSpeed;
}
}
function GetSpeed()
{
return currentSpeed;
}
/**
* (bool) select - to select the unit or not
*/
function SelectUnit(select)
{
resetSelectedUnits();
selected = select;
// add it to the list of selected units
if(selected)
{
selectedUnits.push(this);
}
// remove it from the list of selected units (if it exists)
else
{
var index = selectedUnits.indexOf(this);
if(index != -1)
{
selectedUnits.splice(index, 1);
}
}
// todo: show selection projection
Debug.Log(selectedUnits);
}
function resetSelectedUnits()
{
selectedUnits = [];
}
@script RequireComponent(CharacterController)
@script AddComponentMenu ("Third Person Player/Third Person Controller")