Scale fishing rod in one direction

Hello all,

This may seem like a repeated question, but I have tried all the code possible to achieve what I’m trying. I have a fishing rod and a hook at the end of the rod. Where ever I click on the screen, based on the y index of the click I move my hook to that position. So say for instance my hook is currently at (2,3) if I click at (400, 9) I move my hook to (2,9). So i just move it vertically based on the click.

This is working fine. Now based on the position of the hook I want to scale my fishing rod accordingly. So once im done positioning my hook I call the following ScaleRod(). The rod sprite has a pivot at top-left, and not center. I want to rod to scale only vertically. Below is my code for the same:

    void scaleRod() {
        //get current size of the fishing rod
        var rodLength = System.Math.Abs(renderer.bounds.size.y); 
        //Get distance between the hook and the rod based on y coordinates.
		float hookRodDistance = (System.Math.Abs(transform.position.y) + rodLength) - System.Math.Abs(hookObject.transform.position.y);
		hookRodDistance =  System.Math.Abs(hookRodDistance);
        //Calculate the scale factor
		float scaleFactor = hookRodDistance / rodLength;
		//Scale  the rod		
		transform.localScale = new Vector3(transform.localScale.x, scaleFactor * transform.localScale.y, transform.localScale.z );

The function is giving me wierd output on the scale.

Can someone please help. Have been stuck for a while on this.
Thank you so much.

Change this line:

float hookRodDistance = (System.Math.Abs(transform.position.y) + rodLength) - System.Math.Abs(hookObject.transform.position.y);

to this:

float hookRodDistance = hookObject.transform.position.y - transform.position.y;

And it should work. And i’m not sure why you want to use Math.Abs, maybe you need to remove it.

This is actually about pivot point of your object. In Unity3d, pivot points can not be changed. However, there is really an easy solution :

You can use empty parent GameObject and child is gonna be your fishing rod. Move your empty parent GameObject to edge of your fishing rod which is opposite side of direction which you want to scale and now, you can scale easily parent GameObject to scale your fishing rod in one direction.