Hey, I can’t figure this out on my own, so I thought I might ask for some help. I’m having a problem with my Mouse movement. My character walks where I want it to, but I want to make it move in a grid, with ‘Move Points’. I already have a script which casts a ray from the mouse position and detects collision, but now i want to make my character move over a grid.
My script:
using UnityEngine;
using System.Collections;
public class MovementScript : MonoBehaviour {
public bool isCollider = false;
Vector3 movePosition;
public float speed = 10F;
public Transform player; //The Player
private float startTime;
public float duration = 1.0F;
public int gridSize = 1; //The size of one grid/tile
void Update () {
//The mouse click 'n walk part
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if(Input.GetMouseButtonDown (0))
{
movePosition = new Vector3(hit.point.x,
1.360456F, hit.point.z);
Debug.Log (hit.collider.name + " " + hit.point);
if (hit.collider.name != ("Tile(Clone)"))
{
isCollider = false;
}
else if (hit.collider.name == ("Tile(Clone)"))
{
isCollider = true;
}
if (hit.collider.tag == ("Player"))
{
Debug.Log("Player");
}
}
}
if (isCollider == true)
{
//Here the script for the actual movement
}
}
}
There might have to be a few changes in the ‘movePosition = new Vector3… etc.’ In the game i’m making the player has to walk horizontal and vertical, not diagonal. I really hope someone might be able to help me.
Thanks in advance!