GormenGhast
121 posts
|
Hello everyone.
I have a turret that follows the mouse. I would like it to have an easing effect. All my attempts have failed, so if any of you could help it would be greatly appreciated.
//rotation to mouse
this._rotation = ((Math.atan2(_root._ymouse-this._y, _root._xmouse-this._x)*(180/Math.PI))+90);
//rotation limits
if(this._rotation<-60)
{
this._rotation=-60;
}
if(this._rotation>60)
{
this._rotation=60;
}
This is the code that rotates the turret. If anyone could shed a bit of light as to how it should be altered that would be great.
|
ssjskipp
170 posts
|
if it can only go from -60 to 60, create a ‘rotationValue’ variable and do something like
//Before everything, like, when you load: var rotationValue = this._rotation, just to initiate it.
var targetRotation = ((Math.atan2(_root._ymouse-this._y, _root._xmouse-this._x)*(180/Math.PI))+90);
if (targetRotation > 60){
targetRotation = 60;
} else if (targetRotation < -60){
targetRotation = -60;
}
//rotationSpeed should start at about 5. The purpose of the following line is to remove that jumping around that will happen if the target rotation is closer than the rotation speed, meaning it'll overshoot the target rotation and have to bounce back.
var tmpSpeed = Math.abs(targetRotation-rotationValue) > rotationSpeed ? rotationSpeed : Math.abs(targetRotation-rotationValue);
if (rotationValue < targetRotation){
rotationValue += tmpSpeed;
} else {
rotationValue -= tmpSpeed;
}
this._rotation = rotationValue;
|
GormenGhast
121 posts
|
ssjskipp thank you so much – you are a model human.
Just so that I can confirm I understand it all, the
var tmpSpeed = Math.abs(targetRotation-rotationValue) > rotationSpeed ? rotationSpeed : Math.abs(targetRotation-rotationValue);
lineis saying that the tmpspeed is Math.abs(targetRotation-rotationValue) if it’s less than the rotationSpeed, and rotationSpeed if its greater than it?
Actually writing it out like that has made it gone done be understanded by me.
Thanks a lot!
|