Hudson
Hudson

Reputation: 60

Unity box to Mouse issue

I am having an issue with my code, i'm trying to move a 3D box to the variable of the position of the mouse, I need to know how to change the box's x,y,z with my mouse position script. All im asking really, is how do I change my boxes x,y,z with a variable in another script. Thanks!

Code:

#pragma strict

public var distance : float = 4.5;
var box = Transform;
private var firstObject : cube;

function Start () {
}

function Update () {
       CastRayToWorld();
}

function CastRayToWorld() {
       var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);    
       var point : Vector3 = ray.origin + (ray.direction * distance);    
       Debug.Log( "World point " + point );
       firstObject = GameObject.Find("pos").GetComponent("cube").pos = point;
       firstObject.pos = point;
}

Upvotes: 0

Views: 55

Answers (1)

Benjamin James Drury
Benjamin James Drury

Reputation: 2383

Make sure that the other object is aware of your box gameObject (lets say under the name 'adjustable'), then its simply a case of:

adjustable.transform.position = new Vector3(x, y, z)

To make sure that the object is aware of the boxes gameObject, you could make adjustable a public variable, and then manual drag the box from your scene into the field that would be created in the component on the object in question.

Upvotes: 0

Related Questions