- Home /
The question is answered, right answer was accepted
Scale object based on another object's position
Despite an hour or so of trying to solve it I can't seem to figure out how to calculate the scale factor based on distance. Here's the illustration of my problem.
I've tried manipulating the x values and such, but until now I still cannot figure it out. Here's the snippet:
Vector3 v3Scale = frontWall.transform.localScale;
frontWall.transform.localScale = new Vector3(v3Pos.x*2.0f , v3Scale.y , v3Scale.z);
I use the v3Pos as v3Pos is used to track the mouse position, here it is:
ray = mainCamera.ScreenPointToRay(Input.mousePosition);
float dist;
plane.Raycast(ray, out dist);
Vector3 v3Pos = ray.GetPoint(dist);
I've been stuck on this problem like forever, so any help would be appreciated. UPDATE: Here's the full code,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragDropScript : MonoBehaviour
{
//Initialize Variables
GameObject getTarget;
Ray ray;
bool isMouseDragging;
private Plane plane;
public Camera mainCamera;
void Update()
{
//Mouse Button Press Down
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo;
getTarget = ReturnClickedObject(out hitInfo);
if (getTarget != null)
{
getTarget.transform.position = new Vector3(getTarget.transform.position.x,getTarget.transform.position.y,getTarget.transform.position.z);
isMouseDragging = true;
}
}
//Mouse Button Up
if (Input.GetMouseButtonUp(0))
{
isMouseDragging = false;
}
//Is mouse Moving
if (isMouseDragging)
{
//tracking mouse position.
ray = mainCamera.ScreenPointToRay(Input.mousePosition);
float dist;
plane.Raycast(ray, out dist);
Vector3 v3Pos = ray.GetPoint(dist);
if(getTarget.CompareTag("LeftRightWall") && getTarget.transform.childCount == 1)
{
foreach (GameObject frontWall in GameObject.FindGameObjectsWithTag("TopWall"))
{
Vector3 v3Scale = frontWall.transform.localScale;
frontWall.transform.localScale = new Vector3(v3Pos.x*2.0f , v3Scale.y , v3Scale.z);
}
v3Pos.z = getTarget.transform.position.z;
v3Pos.y = getTarget.transform.position.y;
getTarget.transform.position = v3Pos;
getTarget.transform.GetChild(0).gameObject.transform.position = new Vector3(-v3Pos.x, v3Pos.y, v3Pos.z);
}
if(getTarget.CompareTag("TopWall") && getTarget.transform.childCount == 1)
{
foreach (GameObject leftRightWall in GameObject.FindGameObjectsWithTag("LeftRightWall"))
{
Vector3 v3Scale = leftRightWall.transform.localScale;
leftRightWall.transform.localScale = new Vector3(leftRightWall.transform.localScale.x , leftRightWall.transform.localScale.y , v3Pos.z*2.0f);
Debug.Log("clicked TopWall");
}
v3Pos.y = getTarget.transform.position.y;
v3Pos.x = getTarget.transform.position.x;
getTarget.transform.position = v3Pos;
getTarget.transform.GetChild(0).gameObject.transform.position = new Vector3(v3Pos.x, v3Pos.y, -v3Pos.z);
}
}
}
//Method to Return Clicked Object
GameObject ReturnClickedObject(out RaycastHit hit)
{
GameObject target = null;
ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction * 10, out hit))
{
target = hit.collider.gameObject;
}
return target;
}
}
Right wall is the parent of the left wall, and tagged as LeftRightWall, while the upper wall is the parent of the bottom wall, and tagged as TopWall. Both parents is draggable, thus enabling one another to scale based on the drag distances. Hope it's clear enough.
UPDATE: This is the closest one from what I'm trying to achieve... ((
Here's the code:
float translate = getTarget.transform.position.x - getTarget.transform.GetChild(0).gameObject.transform.position.x;
Debug.Log("value :"+translate);
foreach (GameObject frontWall in GameObject.FindGameObjectsWithTag("TopWall"))
{
Vector3 v3Scale = frontWall.transform.localScale;
frontWall.transform.localScale = new Vector3(translate+v3Pos.x, v3Scale.y , v3Scale.z);
}
What are you trying to accomplish? In other words, what functionality does this serve to your game? Perhaps there is a different, more simple solution.
Sorry, I've edited my comment many times over the past couple $$anonymous$$utes. Could you respond to my new question ins$$anonymous$$d?
I've already updated my question, hope it helps
Have you checked, if the v3Pos values are computed correct? I suspect this is the problem. Perhaps it is possible to use Input.GetAxis https://docs.unity3d.com/ScriptReference/Input.GetAxis.html in your code.
this program is helpful for me a lot. but I am not able to understand complete. can anyone help by making a video or explaining this program please.
Answer by miramaslow · Aug 25, 2017 at 03:55 AM
I just need to use math for it, thanks to this answer. The reasons my walls behaved like that is because all four walls are inside another empty objects. So problem solved.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Problems with GameObjects localScale 0 Answers
Creating objects and positioning next to a previous one 0 Answers