Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by ExpiredIndexCard · Jun 11, 2013 at 08:52 PM · scalelineforwardlong

Scaling in the forward direction not working.

I am trying to make a grapple gun for my 2.5D game. I have a cube that I will use as the rope. How can I scale this cube to make it look like it is shooting out the front of my gun, no matter how the gun is rotated? It should also stop when it reaches the target. Thanks

EDIT: Here is my new code @robertbu

 public class GrappleGunScript : MonoBehaviour {
 
     public float         maxScale    = 7.0f;
     public float         speed         = 45.0f;
     public Transform     ropeSpawn;
     public GameObject     rope;
     
     private bool         hasShot        = false;
     private float         orgScale;
     private float         endScale;
  
     void Start () {
         orgScale = transform.localScale.z;
         endScale = orgScale;
     }
  
     void Update () {
         Ray ray = new Ray(transform.position, transform.forward);
         RaycastHit hit;
         
         if(!hasShot){
             if(Input.GetMouseButtonDown(0)){
                 if(Physics.Raycast(ray, out hit, 15f)){
                     if(hit.collider.tag == "desk"){
                         hasShot = true;
                         Instantiate(rope, ropeSpawn.position, transform.rotation);
                         endScale = hit.point;
                     }
                 }
             }
         }
         if(hasShot)
             rope.transform.localScale = new Vector3(rope.transform.localScale.x, rope.transform.localScale.y, Mathf.MoveTowards(rope.transform.localScale.z, endScale, Time.deltaTime * speed));
     }
 }

At the point where it says endscale = hit.point.z, this part is not working. It does not scale to the end where the raycast hits. How do I fix this? I'm not sure if it's supposed to be on the z axis anyways. What do I do? Also, I only removed best answer so that others can answer as well but I will put it back once it has been answered. Thanks

Comment
Add comment · Show 1
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 robertbu · Jun 14, 2013 at 03:25 AM 1
Share

Assu$$anonymous$$g you are shooting the ray from the same point as the grapple, the calculation will be something like:

 endScale = (hit.point - transfrom.position).magnitude;

In order to make this work, you likely have to start with a local.Z scale of the grapple of near 0.

2 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by robertbu · Jun 11, 2013 at 09:24 PM

This "fires" the cube in the direction of the positive 'z' for the cube. Hit 's' for shoot, and 'r' for rewind. This does not stop when it reaches the target. I'm not sure how you want to handle that situation. You can Physics.Raycast() and reset endScale each frame, or you could do a Physics.SphereCheck() at the position of the point and stop it when it hits something:

 #pragma strict
 var maxScale = 7.0;
 var speed = 15.0;
 
 private var v3OrgPos : Vector3;
 private var orgScale : float;
 private var endScale : float;
 
 function Start () {
     v3OrgPos = transform.position;
     orgScale = transform.localScale.z;
     endScale = orgScale;
 }
 
 function Update () {
     transform.localScale.z = Mathf.MoveTowards(transform.localScale.z, endScale, Time.deltaTime * speed);
     transform.position = v3OrgPos + transform.forward * (transform.localScale.z / 2.0 + orgScale / 2.0);
     if(Input.GetKeyDown(KeyCode.S)) {
         endScale = maxScale;
     }
     else if (Input.GetKeyDown(KeyCode.R)) {
         endScale = orgScale;
     }
 
 }
Comment
Add comment · Show 1 · 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
avatar image robertbu · Jun 11, 2013 at 09:39 PM 1
Share

That line moves the cube in the forward direction to match the scaling so that the rear of the cube stays where it started. If I did not have this line, the cube would appear to grow/shoot both forward and backward. If you are creating your grapple in a 3D program, you can place your pivot at the rear end of the object and get rid of this line. It is only necessary for the standard cube that has the pivot in the center of the cube.

avatar image
1

Answer by ChangeMe12345 · Aug 16, 2019 at 11:00 AM

Thanks to the base, I update it 2019.

     public float maxScale = 10f;
     public float speed = 1f;
 
     private Vector3 v3OrgPos;
     private float orgScale;
     private float endScale;
 
     void Awake()
     {
         v3OrgPos = transform.position - transform.forward;
         orgScale = transform.localScale.z;
         endScale = orgScale;
     }
 
     void Update()
     {
         ResizeOn();
     }
 
     private void ResizeOn()
     {
         transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, Mathf.MoveTowards(transform.localScale.z, endScale, Time.deltaTime * speed));
         transform.position = v3OrgPos + (transform.forward) * (transform.localScale.z / 2.0f + orgScale / 2.0f);
         if (Input.GetKeyDown(KeyCode.S))
         {
             endScale = maxScale;
         }
         else if (Input.GetKeyDown(KeyCode.R))
         {
             endScale = orgScale;
         }
 
     }


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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Dynamic line-drawing between objects (nGUI) 1 Answer

is it possible to scale from one side (not axe) ? 1 Answer

change the length of the vector 1 Answer

Scale Objects become flat. 1 Answer

Bake mesh scale. 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