Hi , can you help me with question. I’ve got this script:
using UnityEngine;
using System.Collections;
public class targetmove : MonoBehaviour
{
Vector3 newPosition;
void Start () {
newPosition = transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
newPosition = hit.point;
transform.position = newPosition;
}
}
}
}
but i need to change that i can only tapp about 1 cm to each side not everywhere! Someone can help?
First, try to use the code tags next time so the code is easier to read. To answer your question you need to check with each click and see if the click happened within your allowed distance, if not don’t do anything.
using UnityEngine;
using System.Collections;
public class targetmove : MonoBehaviour {
Vector3 newPosition;
// the max distance away that you can click. - change the value until you are happy.
int maxClickDistance = 5;
void Start () {
newPosition = transform.position;
}
void Update() {
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
//make sure we didn't click further away than allowed.
if(Vector3.Distance(transform.position, hit.point) <= maxClickDistance){
newPosition = hit.point;
transform.position = newPosition;
}
}
}
}
}