- Home /
Question by
pheekle · Aug 14, 2013 at 03:01 AM ·
movementrotateclicktomove
Rotate character to clicked position
In EVE, the double click to move in space is done by getting a direction vector from where you click on the screen. You aren't actually moving to a point, just in a direction. But what I want it to do is slowly rotate/point to the direction it will be flying towards, then start moving towards that direction. Here is the click to move script I'm using.
#pragma strict
var direction = Vector3.zero;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var mousePos = Input.mousePosition;
mousePos.z = 10000; // select distance = 10 units from the camera
var worldPos = Camera.main.ScreenToWorldPoint(mousePos);
direction = (worldPos - transform.position).normalized;
}
transform.position += direction * Time.deltaTime;
}
Comment
Best Answer
Answer by robertbu · Aug 14, 2013 at 03:12 AM
Here are a couple of changes to your script:
#pragma strict
var speed = 35.0; // Degrees per second
var delay = 2.0;
private var direction = Vector3.zero;
private var qTo = Quaternion.identity;
private var timeStamp : float;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var mousePos = Input.mousePosition;
mousePos.z = 1000; // select distance = 10 units from the camera
var worldPos = Camera.main.ScreenToWorldPoint(mousePos);
direction = (worldPos - transform.position).normalized;
timeStamp = Time.time + delay;
qTo = Quaternion.LookRotation(direction);
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speed * Time.deltaTime);
if (direction != Vector3.zero && timeStamp < Time.time)
transform.position += direction * Time.deltaTime;
}
Your answer
Follow this Question
Related Questions
Updating rotation of client not working - strange error 0 Answers
Click-To-Move like Diablo 2/League of Legends/DoTA/Torchlight 2 Answers
Click to move Diablo Style 3 Answers
Click to move disregards collision? 1 Answer
Help with Grid Movement 2 Answers