So I have a problem with the Shake/Scale with DOTween. When I move the sphere to one tile to the next the scale of the mesh gets deformed. See video below.
http://tinypic.com/player.php?v=358nhwz>&s=9#.Vx2YR5MrLdR
The code that I have is the following. On the tiles I have :
using UnityEngine;
using System.Collections;
using DG.Tweening;
public class TileScript : MonoBehaviour
{
void OnMouseDown()
{
Pawn.instance.transform.DOMove(transform.position,.5f).SetEase(Ease.InOutQuad).OnComplete(ArrivedOnTile);
}
void ArrivedOnTile()
{
Pawn.instance.transform.DOShakeScale(0.5f);
}
}
On the Pawn
using UnityEngine;
using System.Collections;
public class Pawn : MonoBehaviour
{
public static Pawn instance;
void Awake()
{
instance = this;
}
}
Thank you for the help!
But what are yo trying to do?
Youāre Shaking and scaling the Transform.
DOShakeScale deforms the mesh.
Youāre explicitly telling it to do that.
Did you mean to do something else?
Thanks for the response. I want the mesh to shake and once that is finished return to the same exact mesh scale as it was before. What I have currently is overtime the mesh becomes more distorted the more I click on the tiles and I donāt want that to happen. When you look at the video that I uploaded the sphere looks different at the end of the video.
EDIT#
oopsā¦ missread the part where you said you clicked on it REPEATEDLY
Whats happening is that youāre passing the deformed transform to the tween if you click before it returns to original transform.
EDIT# Working code this time LOL
using UnityEngine;
using DG.Tweening;
public class TileScript : MonoBehaviour
{
private Tweener _shakeIt;
private Vector3 _pawnOriginalScale = Vector3.one;
void OnMouseDown()
{
Pawn.instance.transform.DOMove(transform.position, .5f).SetEase(Ease.InOutQuad).OnComplete(() => ArrivedOnTile(_pawnOriginalScale)).SetAutoKill(true);
}
void ArrivedOnTile(Vector3 pawnOriginalTransform)
{
_shakeIt.Kill();
Pawn.instance.transform.localScale = _pawnOriginalScale;
_shakeIt= Pawn.instance.transform.DOShakeScale(0.5f);
}
}
TileTestProject[3.24MB]
1 Like
Wow. Thank you so much FuguFirecracker. Iāll try this out after work and Iāll let you know how it turns out.
Youāll see that the parameter āpawnOriginalTransformā is not actually used in the hastily written code above.
Iām getting the original scale from the hardcoded member variable _pawnOriginalScale.
Itās redundant, but in there for completeness to show how to pass parameters with lambdas ()=>
because I donāt know that you actually want to send it a 1:1:1 scaleā¦ maybe you have pawns of all shapes and sizesā¦
If you do use the method parameter (which should probable be renamed to 'pawnOriginalScale" without the underscore) remember to change the body of the method to use the parameter and not the member variable.
using UnityEngine;
using System.Collections;
using DG.Tweening;
public class TileScript : MonoBehaviour {
private Tweener _shakeIt;
private Vector3 _pawnOriginalScale = Vector3.one;
void OnMouseDown(){
Pawn.instance.transform.DOMove(transform.position,.5f).SetEase(Ease.InOutQuad).
OnComplete(() => ArrivedOnTile(_pawnOriginalScale)).SetAutoKill(true);
}
void ArrivedOnTile(Vector3 OriginalScale){
_shakeIt.Kill();
Pawn.instance.transform.localScale = OriginalScale;
_shakeIt = Pawn.instance.transform.DOShakeScale(0.5f,.4f,10);
}
}
I did what you said and it worked perfectly. Thank you so much. The size of the pawn wasnāt scaled to 1 so I just nested inside of an empty gameobject.
Another question is if there is a way to make the movement duration not set to be always 0.5f but dynamic depending on the distance so the speed will be consistent?
If your Pawn is ALWAYS 1:1:1 scale ( or in this case the GameObject parent) you donāt need to create a member variable, you donāt need to pass any parametersā¦
Change
_shakeIt.Kill();
Pawn.instance.transform.localScale = Vector3.one; // change this line
_shakeIt = Pawn.instance.transform.DOShakeScale(0.5f,.4f,10);
There sure is, but DOTween doesnāt provide any automatic methods to do it.
You have to calculate that somewhere else and supply that variable to your tween.
Hereās a hint:
speed = distance / time
I think I got it Thanks for the hint. 0.5f for time was a bit too slow so I increased it to 5f. Let me know if the following code looks OK or not.
using UnityEngine;
using System.Collections;
using DG.Tweening;
public class TileScript : MonoBehaviour
{
private Tweener _shakeIt;
private Vector3 _originalPos;
private float _distance;
void OnMouseDown()
{
_originalPos = Pawn.instance.transform.position;
_distance = Vector3.Distance(transform.position,_originalPos)/5;
Pawn.instance.transform.DOMove(transform.position,_distance).SetEase(Ease.InOutQuad).
OnComplete(ArrivedOnTile).SetAutoKill(true);
}
void ArrivedOnTile()
{
_shakeIt.Kill();
Pawn.instance.transform.localScale = Vector3.one;
_shakeIt = Pawn.instance.transform.DOShakeScale(0.5f,.4f,10);
}
}
Couple of pointers:
That ā5ā is what is known as a Magic Number
Sure you know what it means now and where it isā¦ but days from now will you remember itās purpose, or what it represents? Magic number can come back to bite you in the ass as your code base grows.
Itās ātimeāā¦ so make a member variable named _time and assign it the value of 5.
Now youāre on your way to āSelf Documenting Codeā.
Also in the interest of self documenting codeā¦ _distance isnāt distance. Thatās SPEED youāve calculated there!
rename _ distance to _speed.
_speed = Vector3.Distance(transform.position,_originalPos)/_time;
1 Like
Right I see what you mean. Iāll change the code based on your feedback. Thanks for everything