- Home /
Change shape of object if selected?
What I am trying to achieve here is to be able to change the shape and size of an object but only if the object has been selected. My script makes sense to me but does not work. The object can be selected but when I use any of the WASD keys to change its shape nothing happens. Can someone help me out?
var selected = false;
var SelectedGameObject : GameObject;
function Update (){
if(Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100.0)){
SelectedGameObject = hit.collider.gameObject;
selected = true;
Change();
Wait(hit.collider.renderer);
}
}
}
function Change (){
if(Input.GetKeyDown(KeyCode.A)){
transform.localScale += Vector3(0.1,0,0);
}
if(Input.GetKeyDown(KeyCode.D)){
transform.localScale += Vector3(-0.1,0,0);
}
if(Input.GetKeyDown(KeyCode.W)){
transform.localScale += Vector3(0,0,0.1);
}
if(Input.GetKeyDown(KeyCode.S)){
transform.localScale += Vector3(0,0,-0.1);
}
}
function Wait(rend : Renderer){
var originalColor = rend.material.color;
rend.material.color = Color.yellow;
yield WaitForSeconds(2);
rend.material.color = originalColor;
}
Answer by dsada · Sep 05, 2014 at 06:18 PM
The problem is that your Change function is inside an Input.GetMouseButtonDown() condition. The only possible way to change the scale of your object if you manage to click and push the button in the same frame. So nearly impossible. What i suggest is something like this:
function Update (){
if(Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100.0)){
SelectedGameObject = hit.collider.gameObject;
selected = true;
Wait(hit.collider.renderer);
}
}
if(selected)
{
Change();
}
}
Thanks works perfectly! Is there a simple way to deselect the selected object if another object has been selected ins$$anonymous$$d?
Well, of course it is pretty easy if you only want to do this.
store a GameObject variable in this class and store there the selected object. in the raycasthit check if the clicked object is the selected one and if it is, then do whatever you want to deselect it. But it pretty much depends on the further plans with this script. But let me sketch an easy solution.
public class SomeClass
{
var selected : GameObject;
function Update (){
if(Input.Get$$anonymous$$ouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100.0)){
SelectedGameObject = hit.collider.gameObject;
if(selected != null && hit.gameObject == selected)
{
selected = null;
}
else
{
selected = hit.gameObject;
}
}
}
Change();
}
function Change (){
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A)){
selected.transform.localScale += Vector3(0.1,0,0);
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D)){
selected.transform.localScale += Vector3(-0.1,0,0);
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W)){
selected.transform.localScale += Vector3(0,0,0.1);
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S)){
selected.transform.localScale += Vector3(0,0,-0.1);
}
}
}
Please forgive me if there are syntax errors i never code in javascript. But thepoint is understandable i guess
I have opened a new question for this. Thanks for the help.
Your answer
Follow this Question
Related Questions
Deselect a selected object? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Destroy only selected object 1 Answer
Help with hiding an object on trigger enter 1 Answer
Trouble grabbing and holding a cube. 0 Answers