- Home /
How to stop a unit from rotation once it gets to designated position?
Hello guys, I'm having troubles.. With the script I have so far, when the unit reaches its designated position, it still keeps rotation itself standstill..
This is the code:
if (target != this.transform.position)
{
// find the vector pointing from our poisiton to the target
directionToMove = (target - this.transform.position).normalized;
// create the rotation we need to be in to look at the target
lookRotation = Quaternion.LookRotation(directionToMove);
// rotate us over time
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
this.transform.position += this.transform.forward * moveSpeed * Time.deltaTime;
isMoving = true;
}
Any ideas? Thanks!
Answer by Griffo · Jan 20, 2013 at 07:33 PM
Adapt this -
#pragma strict
var distance : float;
var target : Transform;
var lookAtDistance : int = 15.0;
var attackRange : int = 10.0;
var standOffDistance : float = 2;
var moveSpeed : int = 5.0;
var damping : int = 6.0;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
lookAt ();
}
if((distance < attackRange) && (distance > standOffDistance))
{
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function attack ()
{
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}
Thank you, actually both of your works have helped me, and I managed to combine it together and get a working result! Thanks!
Answer by sparkzbarca · Jan 20, 2013 at 06:20 PM
lookRotation = Quanternion.LookRotation(directionToMove - transform.forward)
mark as answered and have a nice day :)
Not so fast cowboy :D I'd be more then happy to just copy-n-paste it, and mark your post as answered and go on having fun :)
But, it won't work.. With your code attached (modified), the only difference is that tanks moves in a jerkish fashion toward position, and once it gets there, he starts rotating itself once again :(
wait are you saying it keeps rotating always or it rotates once you you arrive at your destination?
I think you mean once you arrive you rotate in circles.
Thats because it never reaches its destination perfectly and thats fine. It's hard to hit something to within millionths of a meter.
undo earlier and keep lookrotation (directionTo$$anonymous$$ove)
change
if(target != transform.position)
to
if(vector3.distance(target,transform.position) > .01f) or any really small number really.
Basically you just want to stop rotating if the distance between the taret and the position is sufficiently small.
mathf.epsilon produces a REALLY REALLY small number
like .1 * - 10^42
but that might be too fine. that distance is in meters
.02 meters = 1 inch approx
so .01 is actually less than half an inch its .4 inches or so
you could probably set it even as high as .05 and that'd stop rotating within about 2 to 2 1/2 inches.
so yea just
if(vector3.distance(target,transform.position) > acceptable_distance_in_meters)
Thank you, actually both of your works have helped me, and I managed to combine it together and get a working result! Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613091252im_/https://answers.unity.com/themes/thub/images/avi.jpg)