Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 25, 2017 at 06:23 AM by miramaslow for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by miramaslow · Aug 22, 2017 at 08:44 AM · c#raycastingtransform.positiondragginglocalscale

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.

alt text

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... ((

alt text

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);
                 }


cube2.gif (139.9 kB)
cube.gif (108.2 kB)
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DJT4NN3R · Aug 22, 2017 at 08:52 AM 0
Share

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.

avatar image miramaslow DJT4NN3R · Aug 22, 2017 at 08:54 AM 0
Share

It's a long code, but I'll post it.

avatar image DJT4NN3R miramaslow · Aug 22, 2017 at 09:01 AM 0
Share

Sorry, I've edited my comment many times over the past couple $$anonymous$$utes. Could you respond to my new question ins$$anonymous$$d?

avatar image miramaslow DJT4NN3R · Aug 22, 2017 at 09:05 AM 0
Share

I've already updated my question, hope it helps

avatar image Destolos miramaslow · Aug 22, 2017 at 09:09 AM 0
Share

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.

Show more comments
avatar image krishsow88 · Oct 03, 2017 at 04:46 AM 0
Share

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.

1 Reply

  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

376 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Unity crashing and LocalScale not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges