x


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 )

more ▼

asked Jul 22 '12 at 08:40 AM

SergeantBiscuits gravatar image

SergeantBiscuits
181 7 12 18

Surely if the chassis rotates and it is the parent then the turret rotates with it?

Jul 22 '12 at 09:02 AM whydoidoit

Yes, but I want the turret to be able to rotate independently from the tank by mouse control.

Jul 22 '12 at 11:05 AM SergeantBiscuits

added an edit with a better solution for that see my answer :)

Jul 22 '12 at 11:27 AM Seth Bergman
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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!

more ▼

answered Jul 22 '12 at 09:22 AM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

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!

Jul 22 '12 at 09:54 AM SergeantBiscuits
(comments are locked)
10|3000 characters needed characters left

Well from what I gather from your post, you could do something like:

 var movementScript = transform.parent.GetComponent("Movement");
 turretAngle = movementScript.directionAngle;
more ▼

answered Jul 22 '12 at 10:02 AM

xSpectrum gravatar image

xSpectrum
90 6 13 25

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x816
x418
x407
x78

asked: Jul 22 '12 at 08:40 AM

Seen: 651 times

Last Updated: Jul 23 '12 at 04:03 AM