I’ve been searching the forums and answers to try and solve point and click movement which I believe I have going okay. I however ran into a problem with the camera angle, which I think has been messing with me getting the right place for the character to move to. Someone else suggested this in another post about doing a similar thing but never left an actual solution. I’ll throw up the code I have code I have going and a screenshot of the camera angle.
I could be wrong about the problem but any help would be great. Thanks in advance.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class CharacterClickController : MonoBehaviour
{
RaycastHit hit;
Vector3 newPos;
bool canWalk = false;
public float speed = 3.0f;
public float rotateSpeed = 3.0f;
// Use this for initialization
void Start ()
{
hit = new RaycastHit();
}
// Update is called once per frame
void Update ()
{
if (canWalk)
{
makeCharacterWalk();
}
}
void OnGUI()
{
Event e = Event.current;
if (e.button == 0 && e.isMouse)
{
Debug.Log("mouse click");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000.0f))
{
newPos = new Vector3(hit.point.x, 0.5f, hit.point.z);
}
canWalk = true;
//point = Input.mousePosition;
//Debug.Log("point is " + point);
}
}
void makeCharacterWalk()
{
CharacterController controller = GetComponent<CharacterController>();
controller.SimpleMove(newPos * Time.deltaTime);
}
}