x


Can't see effect of setting scale in script

Hi, I've just started using Unity3d and I'm having some problems with setting the scale of an object in a script. I have a gameObject with two scripts, Selectable and Moveable. (I'm making a turn based strategy game) Selectable allows the user to select an object by clicking it which works perfectly. Moveable is supposed to allow the user to move an object, but first I want to draw a plane with a texture from the object to the mouse to show the movement path, I want to scale it's y value to the length of the move (divided by 10 because of its size) and when that works I'll rotate and translate it but I've run into problems when I try to scale it. Setting the scale just after I've created it seems to work perfectly (see the comment in the code, it scales to what ever I set it to) but when I move the mouse around nothing happends (see the second comment at the bottom) even though I set the scale in the same fashion as before and the Log.Debug print out shows me that the scale is changing according to the distance of the mouse from the object.

It's probably something simple that I've overlooked but I can't find it for the life of me. Any help will be greatly appreciated!

using UnityEngine; using System.Collections;

public class Moveable : MonoBehaviour {

 public GameObject walk_path;
 // Use this for initialization
 void Start () 
 {
     walk_path = null;
 }

 // Update is called once per frame
 void Update () 
 {
     if (gameObject.GetComponent<Selectable>().GetSelected())
     {
         if(walk_path == null)
         {
             Vector3 pos = new Vector3(gameObject.transform.position.x, 0.001f, gameObject.transform.position.z);
             walk_path = Instantiate(Resources.Load("WalkPath"), pos, gameObject.transform.rotation)  as GameObject;
             walk_path.transform.localScale = walk_path.transform.localScale * 10.0f; //<--when I set it here the effect is visible in the game
         }
         if(walk_path)
         {
             Ray mouse_ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             Debug.DrawRay(mouse_ray.GetPoint(0.0f),mouse_ray.GetPoint(100.0f), Color.green);
             float dist = 0;

             Vector3 position = GameObject.Find("Ground").transform.position;
             Plane plane = new Plane(Vector3.up, position);

             if (plane.Raycast(mouse_ray, out dist))
             {
                 Vector3 new_pos = new Vector3();
                 new_pos.x = mouse_ray.GetPoint(dist).x;
                 new_pos.y = 0.001f;
                 new_pos.z = mouse_ray.GetPoint(dist).z;

                 Vector3 pos = new Vector3(gameObject.transform.position.x, 0.001f, gameObject.transform.position.z);
                 float length = (new_pos - pos).magnitude;

                 Vector3 local_scale = walk_path.transform.localScale;
                 local_scale.y = length * 0.1f;
                 walk_path.transform.localScale = local_scale; //when I set it here the effect does nothing even though the debug.log writes out different scales in the debug.log below
                 Debug.Log("walk_path localScale: "+walk_path.transform.localScale);
             }
         }
     }
 }

}

more ▼

asked Feb 10, 2011 at 10:46 PM

StartSelect gravatar image

StartSelect
1 3 2 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Just solved it, cant scale y value of billboard laying on ground. :( so simple.

more ▼

answered Feb 11, 2011 at 12:20 AM

StartSelect gravatar image

StartSelect
1 3 2 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9517
x851
x710

asked: Feb 10, 2011 at 10:46 PM

Seen: 2081 times

Last Updated: Feb 10, 2011 at 10:46 PM