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.