- Home /
reset an object's position doesn't work fine
I'm doing a fishing game where you grab fishes, there 2 Cases:
1-transform the hook to a random place, hit nothing and come to Hook_Source(type=transform). 2-collide with a fish and both objects (fish and hook) transform to Hook_Source.
In order to grab many times, the condition hook.transform.position=Hook_Source.position must be true.
In the first case the hooking (not really hooking) operation works fine and the hook position reset after I transform it. but in the second case the problem is the hook operation works fine, but the hook don't reset to Hook_Source, there's about -+0.1 difference in distance don't know why, which affects my second hook operation, here's a video explains the situation :
my Collision script ( the script is attatched to the hook(sprite)):
void OnCollisionStay2D(Collision2D coll){
if(coll.gameObject.tag == "fish"){
fish = coll.gameObject;
fish.transform.position = this.transform.position;
StartCoroutine(moveHookBack(Hook_Source, 10f));
}
if(Vector3.Distance(this.transform.position, Hook_Source.position) < 2){
Destroy(fish);
this.transform.position = Hook_Source.position;
}
}
Answer by IMemeManI · Jun 23, 2017 at 09:04 AM
I had this problem, it kept morphing my item and it didn't go to the position I wanted.
My solution: Create an empty gameobject and attach it to the position you wish, make it a child of the hook and add a collider, make it a trigger, when the "Fish" colliders with it instead of making it's position relative to the hook, it'll make it relative to the empty object which will stop object morphing and will make it attach to the position you wish.
GameObject Fish;
void OnTriggerEnter(Collider coll)
{
(if coll.gameobject.tag == "Fish")
Fish = coll.gameobject; //Now it can be used.
Fish.transform.position = this.transform.position; /* makes it 0,0,0 on the empty*/
}
Thanks for the suggestion, but I think you either didn't understand well my problem or I didn't explain enough. I made a gameobject ( Hook_Source) using only its position to reset the Object Hook (sprite) to it after every hook. In the video at 12s you can notice the transform.position of the Hook-sprite morphing a bit before I click again and it affect my next hook (doesn't go where I click, in order to go where I click, I need Hook-sprite=Hook_Source).
I tried a bit to do you suggest but didn't get it well, I just want this piece of code this.transform.position = Hook_Source.position;
works
$$anonymous$$aybe my moveHook function code will help to get things clear:
IEnumerator moveHookBack(Transform end, float S){
while(transform.position != end.position ){
this.transform.position = Vector2.$$anonymous$$oveTowards (this.transform.position, end.position, S * Time.deltaTime);
yield return new WaitForEndOfFrame();
}
}
Your answer
