C# Scale in one direction

I am instantiating an object that I want to stretch between two different points that I will use to draw a path. Right now the object is a plane and I have made it the child of an empty and shifted the picot point so when I scale it in the editor it only stretches in one direction, the intended result. However, whenever I do it with this script, it still scales in both directions making unwanted behavior. Any suggestions on how I can change the code below?

public class path_Drawing : MonoBehaviour
{
    public bool isPathing = false;
    private Vector3 startPoint;
    private Vector3 mousePosition;
   // private Vector3 prevMousePosition;
    public GameObject path;
    public GameObject endpoint;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isPathing = true;
            startPoint = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20f));
        }

        if (isPathing)
        {
            transform.position = startPoint;
            mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20f));
            float distance = Vector3.Distance(startPoint, mousePosition);
            Debug.Log(distance);
            transform.localScale = new Vector3(distance, 1f, 1f);
            transform.position = (startPoint + mousePosition) / 2f;
            //fix the scale factor and have it scale in one direction
        }
       // prevMousePosition = mousePosition;

        if (Input.GetMouseButtonUp(0))
        {
            isPathing = false;
        }
    }
}

Uhm, did you attach this script to the empty parent object or the object itself? Currently this script changes the scale the script is attached to. So if it’s on the child. Of course it would scale the child and not the parent. If that’s the case you have two options:

  1. Either attach the script to the parent instead of the child object
  2. Or change the script so it doesn’t scale the object the script is on but the parent objects of that object. Keep in mind that your movement (setting the position) should also be done on the parent. When you move the child you would mess up the alignment you set up in the editor.

I fixed the error. I had the script on the parent object and the object was a child correctly. However, I left some legacy script from a previous method I had tried that was causing it to recalculate the start position which was scaling the distance way up because it would break the if logic before it would calculate correctly. I’ve posted the corrected code below.

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.XR;
using UnityEngine;

public class path_Drawing : MonoBehaviour
{

    public bool isPathing = false;
    private Vector3 startPoint;
    private Vector3 mousePosition;
    public GameObject path;
    public GameObject endpoint;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isPathing = true;
            startPoint = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20f));
            transform.position = startPoint;
           
        }

        if (isPathing)
        {
            Debug.Log(startPoint);
            mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20f));
            float distance = Vector3.Distance(startPoint, mousePosition);
            transform.localScale = new Vector3(distance, 1f, 1f);
                      
            
        }
       

        if (Input.GetMouseButtonUp(0))
        {
            isPathing = false;
        }


    }
}