- Home /
Mouse2Move Not Working?
Hello, I'm trying to get this point and click script working. In my TP_MoveByMouse.cs I have the following code
using UnityEngine;
public class TP_MoveByMouse
{
private static TP_MoveByMouse singelton = null;
private Vector3 movePoint = new Vector3(0,0.9f,0);
private float distance = 0;
private TP_MoveByMouse()
{
}
public static TP_MoveByMouse GetInstance()
{
if(singelton != null)
{
return singelton;
}
var x = new TP_MoveByMouse();
return x;
}
public void MoveByMouseKlick()
{
var player = GameObject.Find("Capsule");
var playerCurrentPos = player.transform.position;
if (Input.GetMouseButton(0))
{
movePoint = RayCastLineTest(playerCurrentPos);
GetDistance(player);
playerCurrentPos = player.transform.position;
if (!player)
return;
}
if(distance > 0.1f)
{
var speed = 10f;
var moveSpeed = speed * Time.deltaTime;
var moveStep = movePoint - player.transform.position;
moveStep.Normalize();
player.transform.LookAt(movePoint);
player.transform.position = playerCurrentPos + (moveStep * moveSpeed);
playerCurrentPos = player.transform.position;
GetDistance(player);
}
}
private void GetDistance(GameObject player)
{
distance = Vector3.Distance(player.transform.position, movePoint);
Debug.Log(string.Format("Distance: {0}", distance));
}
public Vector3 RayCastLineTest(Vector3 playerLoc)
{
var hit = new RaycastHit();
Ray ray = (Camera.main.ScreenPointToRay(Input.mousePosition) );
Physics.Raycast(ray, out hit);
Debug.DrawLine(Camera.main.transform.position, hit.point);
return hit.transform.tag == "CanMoveOn" ? new Vector3(hit.point.x,hit.point.y + 0.9f,hit.point.z) : playerLoc;
}
}
And I'm calling it and instantiating it in controller_2.cs. It's attached to a capsule. Every time it runs I the debug distance shows "0 distance" and wont move. Am I calling it wrong?
controller_2.cs
using UnityEngine;
using System.Collections;
public class controller_2 : MonoBehaviour {
TP_MoveByMouse move = TP_MoveByMouse.GetInstance();
// Update is called once per frame
void Update ()
{
move.MoveByMouseKlick();
}
}
Any help would be greatly appreciated!!!
Answer by sparkzbarca · Jul 09, 2013 at 01:10 PM
what is raycastlinetest. Need to see the code also dont move like that move using lerp.
void update() { if (distance > .1) { player.transform.position = vector3.MoveTowards(player.transform.position,movepoint, speed * time.deltatime); } } that will move smoothly towards the target.
Your answer
Follow this Question
Related Questions
Why does this not detect mouse movement? 1 Answer
Trying to move a instantiate gameobject with mousedown 1 Answer
Imported skehup into unity not moving as I want!!! 2 Answers
How Do Change This Code to X-Y ? 1 Answer
button.Select() and alternatives don't seem to work when called via new input system 1 Answer