Ok I am having a problem I do not know if it is in my raycast or in my move unite code can any one help me.
The parts that work are, I can click on my unit and have it be selected that is np. But when I click to move the unit it will not move I do not know what I did wrong I am not getting any errors. This is my code. ps this is not for a game I am gust trying to get a RTS mover to work I got the A* pro(free) and I got that to work I am trying to get the ray cast to work and move a target around so I can have a target for the A* that moves with a click. but first I want to figer this out how to just move the unite to a point using raycast.
using UnityEngine;
using System.Collections;
public class UnitSelectUnit : MonoBehaviour
{
//UnitSelect prerec
public bool selected = false;
private bool selectedByClick = false;
private Vector3 moveToDest = Vector3.zero;
public float floorOffset = 1;
public float speed = 5;
public float stopDistanceOffset = 0.5f;
//UniteSelecting code
private void Update ()
{
if (Input.GetMouseButton(0))
{
if(!selectedByClick)
{
Vector3 camPos = Camera.mainCamera.WorldToScreenPoint(transform.position);
camPos.y = UnitSelectCamera.InvertMouseY(camPos.y);
selected = UnitSelectCamera.selection.Contains(camPos);
}
if (selected)
{
}
else
{
}
if(selected Input.GetMouseButtonUp(1))
{
Vector3 destination = UnitSelectCamera.GetDestination();
if (destination != Vector3.zero)
{
//if you have unity pro us
// gameObject.GetComponent<NavMeshAgent>().SetDestination(destination);
//if you do not have unity pro us
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
{
if(rigidbody)
transform.rigidbody.velocity = Vector3.zero;
}
}
private void OnMouseDown()
{
selectedByClick = true;
selected = true;
}
private void OnMouseUp()
{
if (selectedByClick)
selected = true;
selectedByClick = false;
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UnitSelectCamera : MonoBehaviour
{
public GUIStyle MouseDrageSkin;
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"};
void Update ()
{
CheckCamera();
Cleanup();
}
private void CheckCamera()
{
if (Input.GetMouseButtonDown(0))
{
startClick = Input.mousePosition;
}
else 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;
}
}
private void OnGUI()
{
if (startClick != -Vector3.one)
{
GUI.Box (selection,"",MouseDrageSkin);
}
}
public static float InvertMouseY(float y)
{
return Screen.height - y;
}
private void Cleanup()
{
if (!Input.GetMouseButtonUp(1))
moveToDestination = Vector3.zero;
}
public static Vector3 GetDestination()
{
if(moveToDestination == Vector3.zero)
{
RaycastHit hit;
Ray r = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(r, out hit))
{
while (!passables.Contains(hit.transform.gameObject.name))
{
if (!Physics.Raycast(hit.point + r.direction * 0.1f, r.direction, out hit))
break;
}
}
if(hit.transform != null)
moveToDestination = hit.point;
}
return moveToDestination;
}
}
sorry about that code not being in a code tag and ty for the link to the page for the help