- Home /
The most siplest thing will not work..
edit(Bunny83)
Copied and merged from comment:
var Firefrom : Transform;
var currentbrick: GameObject;
var finalbrick : GameObject;
;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
var hit : RaycastHit;
var roundpos : Vector3;
if(Physics.Raycast(Firefrom.position, Firefrom.forward, hit))
{
//roundpos =
Debug.Log(hit);
roundpos = hit.point - Firefrom.forward.normalized * 0.05;
roundpos *= 2.0;
roundpos = Vector3(Mathf.Round(roundpos.x), Mathf.Round(roundpos.y), Mathf.Round(roundpos.z));
roundpos /= 2.0;
var Temp = Instantiate(currentbrick, roundpos , Quaternion.LookRotation(Vector3.zero));
if(Input.GetKeyDown(KeyCode.W))
{
Temp.tranform.position = Vector3(1,2,3);
}
}
}
}
The last part will not work, there is literaly no way to move the thing. (this isn't full script)
You need to show more of the script as you claim this is not all of it. Also, you need to tell the error or problem.
I would suspect that the instantiation happens in a method and you get that Temp does not exist in this scope.
var Firefrom : Transform;
var currentbrick: GameObject;
var finalbrick : GameObject;
;
function Update () {
if(Input.GetButtonDown("Fire1")){
var hit : RaycastHit;
var roundpos : Vector3;
if(Physics.Raycast(Firefrom.position, Firefrom.forward, hit)){
//roundpos =
Debug.Log(hit);
roundpos = hit.point - Firefrom.forward.normalized * 0.05;
roundpos *= 2.0;
roundpos = Vector3($$anonymous$$athf.Round(roundpos.x), $$anonymous$$athf.Round(roundpos.y), $$anonymous$$athf.Round(roundpos.z));
roundpos /= 2.0;
var Temp = Instantiate(currentbrick, roundpos , Quaternion.LookRotation(Vector3.zero));
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W)){
}
}
}
}
Cant belive I can not do this crap.
Answer by Bunny83 · Dec 05, 2013 at 03:25 PM
Well your code can't work. Update is called once every frame. However this if statement is only true for one frame, the moment you press your fire button:
if(Input.GetButtonDown("Fire1"))
So everything within the body of the is block is executed "once" the moment you press fire.
Inside the body you have another "button down" check for the "w" key, however it's nearly impossible that you press both keys at the exact same time. If you press w 10ms later the next frame the first if statement is false again.
It seems you're lacking of some basic control flow rules. From your script it's hard to tell what you actually want to do... You clamp the position to a multiple of 2, which is fine, but why do you want to set the position of the instantiated "brick" to a fix world position when you press a button?
Keep in mind that you can press fire multiple times which would instantiate multiple bricks. If you want to somehow "move" the brick with a key press, which brick do you want to move? the last created?
First you should be clear what you want to do and once that's clear you start coding it. If you can't explaint what you want to do, you can't code it!
I know. But I have an idea of what I want to do. Only problem is, I cant ad a vector3 valse for some odd reason.
meaning I can do all this, but Temp.transform.position += Vector3(1,1,1); wont work. er
That's intended @applejuices. If you want to move something use Temp.transform.Translate( Vector3( 1, 1, 1 ) );
Your line:
Temp.transform.position += Vector3(1,1,1);
works pretty well.
The problem is that this line is never executed, because in your code it would only execute when you press both keys at the exact same time, which would almost never happen.
You have "an idea" what you want to do, but you haven't told us your "idea". We told you why your code doesn't work, but we can't suggest a solution without knowing what you want. Try to explain what exactly should happen when you do what. $$anonymous$$eep in $$anonymous$$d to not use general terms like "the object should move". Which object? Again, if you press your fire button 2 or more times you have 2 or more objects in the scene. When you press the "W" key which object(s) should do what.
You're giving us a hard time to help you...