- Home /
Vector3 of moving object always returns (0,0,0)
Got a slight issue with finding an objects position and I'm not sure what the problem is.
The purpose of the scripts below is to instantiate an object at the player's position, slow down time on buttonDown and move the player to the object's location on buttonUp (and resume time). However, the position of the object is always returning as (0,0,0) and not the actual position of the object as it is on the screen. It probably has something to do with the movement script of the object, but for clarity's sake I pasted both of the scripts which are used below.
If you have any idea how I could resolve this issue it'd be greatly appreciated! Thank you for your time.
This is attached to the player:
#pragma strict
var locaterP1 : Transform;
var targetPosition : Vector3;
var found : boolean;
function Update (){
print(targetPosition); // this is what I want to know, but always returns (0,0,0)
if (GameObject.Find("locaterP1(Clone)")){
var locater = GameObject.Find("locaterP1(Clone)");
targetPosition = locater.transform.position;
}
if(Input.GetButtonDown("P1RightBumper")){
Time.timeScale = .03;
Instantiate(locaterP1, transform.position, transform.rotation);
}
if(Input.GetButtonUp("P1RightBumper")){
Time.timeScale = 1;
transform.position = targetPosition;
}
}
This is attached to the instantiated object, ie the locater:
#pragma strict
var speed : int = 10;
function Update (){
var lh = Input.GetAxis("P1HorizontalMovement") * speed * Time.deltaTime;
var lv = Input.GetAxis("P1VerticalMovement") * speed * Time.deltaTime;
var pos : Vector3 = transform.position;
// this moves the locater, but apparently doesn't allow finding it's Vector3?
var dist = (transform.position.y - Camera.main.transform.position.y);
var leftLimitation = Camera.main.ViewportToWorldPoint(Vector3(0.0177,0,dist)).x;
var rightLimitation = Camera.main.ViewportToWorldPoint(Vector3(.983,0,dist)).x;
var upLimitation = Camera.main.ViewportToWorldPoint(Vector3(0,0.0277,dist)).z;
var downLimitation = Camera.main.ViewportToWorldPoint(Vector3(0,.97,dist)).z;
// this is some gunk to limit movement, not relevent necesarily
pos.x = Mathf.Clamp(pos.x + lh, rightLimitation, leftLimitation);
pos.z = Mathf.Clamp(pos.z + lv, downLimitation, upLimitation);
transform.position = pos;
}
As a final note, I'm fairly new to Unity, so if you could take that into regards when answering it'd be really appreciated.
Answer by Deathcalibur · Feb 04, 2013 at 05:04 PM
Hello,
I think the problem is that your top script is not finding the locator and is thus not updating your target position. Try debugging your script by printing the targetposition's x y and z values before assigning them to transform.position (or alternately try printing out the locator.transform.position).
The values default to 0,0,0. Also, try printing on line 12 or so inside your if statement something like "I found the locator" which may help you determine if this is where the problem lies.
Let me know if this helps you figure things out.
Ah damnit, silly mistake. I did actually debug to find it before, and it worked then, but somewhere along the way it got switched out with another object to instantiate. I was actually using the P2 object rather than the P1 object, so I instantiated locaterP2 rather than locaterP1. No idea when that happened. Amazing how silly mistakes can just screw you over haha. Thanks for the hint!
When in doubt you can keep a bunch of print statements around and comment them out later if you don't need them any more. It generally helps me a lot.