Rotate a sprite ( card ) until it reaches the destination?
I am making a card game. I am trying to create an effect when player plays his/her card. I would like to rotate the card as it moves to the destination ( another gameObject in middle of the board ). When card reaches the destination it should be aligned with the destination's rotation.
I tried following code without success. The card seems to rotate as long as it reaches the destination's rotation. I want continious rotation
void Start(){
rotateAtoB=true;
startDistance=Vector3.Distance(transform.position,endPos.transform.position);
}
void Update(){
if(rotateAtoB){
float delta= 1 - Mathf.Pow(Vector3.Distance(transform.position,endPos.transform.position)/startDistance,5.0f/9.0f);
transform.rotation=Quaternion.Slerp(transform.rotation,endPos.transform.rotation,(delta/startDistance)*1.0f);
if(Mathf.Approximately(delta,1)){
rotateAtoB=false;
}
}
transform.position=Vector3.MoveTowards(transform.position,endPos.transform.position,2.0f*Time.deltaTime);
}
I am also thinking about AddTorque method, but not sure if this is what I want. Any ideas how to move forward?
Answer by Soos621 · May 31, 2016 at 08:17 PM
I would suggest use a tween script to move the card to the board this will alliw you to easily animate the movement as for the rotation you seem to have the correct script going with the quaternion.slerp but I wouldn't set the sleeps variable to 1 set it to something like .1 at first this will rotate the card more slowly
Itween is available for free on the unity asset store
Your answer
Follow this Question
Related Questions
Preserving the players rotation on rotating platforms 0 Answers
Flipping Sprite Renderer as of unity 5.3 with negative scale effects lighting when using bump map. 2 Answers
Linear speed in one direction 0 Answers
Polygon Collider 2D in 3D space missmatch 0 Answers
Game (2D) not rendering at all 2 Answers