- Home /
How to read variable from parent object?
Sorry if this is an exceedingly simple question; somehow haven't been able to find an answer.
I have a parent object, "TankChassis", with the script "Movement" (javascript) that contains a variable "directionAngle" that describes the angle it is facing (y component.) The tank chassis is driven around with WASD.
Within TankChassis I have "TankTurret", with the script "TurretControl" that rotates the turret between -110 and +110 degrees (eulerAngles.y) depending on the x position of the mouse.
What I want to do is add directionAngle from TankChassis to the angle of the turret so that the turret rotates in relation to the tank chassis.
What's the best way to do this? Tl;dnr - how do I get a child's script to read a variable from a script in its parent?
(I'm learning Unity by brute force; hope I'm not bothering anyone with simple questions!)
Thank you so much!
(A picture of my cute, derpy tank for your trouble: http://i46.tinypic.com/fnten7.png )
Surely if the chassis rotates and it is the parent then the turret rotates with it?
Yes, but I want the turret to be able to rotate independently from the tank by mouse control.
added an edit with a better solution for that see my answer :)
Answer by Seth-Bergman · Jul 22, 2012 at 09:22 AM
Edit: moved from comments, really more of an answer i guess
must be taking direct input off of the mouse position, in which case.. if you really want to, you could add a var and pass the difference of the tanks rotation to it..
offset = currentTankRotation - originalTankRotation ;
turret.rotation = mousePositionX + offset; //pseudocode
oh,and as for how to read the var,you could just say:
var tank : Transform;
tank = transform.parent;
var tankYRotation = tank.eulerAngles.y; //for example
once you have that, you can access other scripts you're after too, with
var myScript = tank.gameObject.GetComponent(MyScript); // MyScript = the name of the script
myScript.hitPoints = 100; // var (of type int) on myScript
myScript.DoSomething(); // function on myScript
EDIT:
one good way would be to just use the distance of mouse movement to apply the rotation:
var lastMousePos : float;
var currentMousePos : float;
function Start()
{
currentMousePos = Input.mousePosition.x;
lastMousePos = Input.mousePosition.x;
}
function Update(){
currentMousePos = Input.mousePosition.x;
var thisFrameRotate = currentMousePos.x - lastMousePos.x;
transform.Rotate(Vector3.up, thisFrameRotate);
lastMousePos = Input.mousePosition.x;
}
not tested but should work, at least to give an idea
and glad to help!
Thank you so much; I had seem similar solutions but for some reason the way you laid it out finally made it click in my head. Starting to understand how this javascript stuff works, haha. Thanks again!
Answer by xSpectrum · Jul 22, 2012 at 10:02 AM
Well from what I gather from your post, you could do something like:
var movementScript = transform.parent.GetComponent("Movement");
turretAngle = movementScript.directionAngle;