Need help trying to make a door open

Okay here is my problem, iv done this script so that when the mouse is over the door it will be a different color to what i choose then if I click on a door the door will open, only all it is opening is the collider and not updating the mesh that should be going with it :confused: anyone have any ideas? or have I missed putting something in my script, I’m really hitting a brick wall with this one

var Color : Color;
var Transform : Transform;

var smooth = 2.0;
var DoorOpenAngle = 90.0;

private var open : boolean;

private var defaultRot : Vector3;
private var openRot : Vector3;

function Start(){
defaultRot = transform.eulerAngles;
openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
}
function OnMouseEnter ()
	{
		renderer.material.color = Color; 
	}
	function OnMouseExit()
	{
		renderer.material.color = Color.white;
	}      	

	function OnMouseDown ()
	{
	if (Transform)
	{
	open = !open;
	}
	}
function Update (){
if(open){

transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
}else{

transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
}
}

Well are the mesh on the ā€œsameā€ object?

Also this seems weird

var Color : Color;
var Transform : Transform;

But i am used to C# so maybe i am wrong.

Another way you can do this is, you can animate the door (you can do this in unity or another 3d software) then call the animation when you need it.

PS: Please move on to C# :slight_smile:

yeah iv put the collider on the mesh then put the script on the mesh so its all on the same object but with no luck,
also the color : color allows me to select a color that i want the door to be highlighted in when the mouse is over it
as for tansform i just added that in to see if it would change anything but no luck :confused:

i really should move on to C# only I don’t get on with it at the moment :stuck_out_tongue:
i could try the animation way though just gotta find out how to call it in the script :slight_smile:

It does seem like your names for color and transform should be different. OnMouse events only work on the object the script is attached too.

the color works fine the transform iv taken out because it does actually seem to do anything anyway
its just the script seems to open the collider rather than the mesh the collider is on

If the collider is moving without its parent, you may need to either reference the parent (Door GameObject) rather than just the collider, or attach the script to the parent door. If you move the parent, all of the child objects should move with it.

I also agree that

var Color : Color;
var Transform : Transform;

seems odd, but I’m no JS programmer.

You should never mask classes with variable names unless you have a reason.

var color : Color; is the proper way as you don’t hide the original class name. Bad practice to do var Color : Color; even tho it is perfectly legit, you could end up running in to trouble later on.

so i am guessing you made a new child and placed a collider on it, and on that child you placed your door script. which will move the transform your collider is on, best thing to do is have var Target : Transform; as a property, set it in the inspector as the Parent of the Door Mesh and Door Collider, and in update Rotate the Target. this way you’ll move the collider and the mesh in the same time.

Try it see if it fixes your problem.

Yeah i get what you are saying only i need a mask colour when the mouse goes over objects to say it can be selected and i need like several different colours for different objects so the ā€œcolor:colorā€ allows me to select a mask colour in unity just saves me making loads of scripts per one :slight_smile: (note iv made it into a separate script on its own now) not aware of any problems it could cause later on though?

Well iv had a play around with it last night done some animations on maya and put a door script on the door and a input handler on the door to call at animations when clicked on, seems too work but its a bit tricky when the door is facing a different direction, so i will give your way a goo too, thank you

You can make simple animations in Unity. since all you need to do is rotate objects. the tricky part is to parent your objects correctly.
For example the door Mesh object, collider object and so on… needs to be parented to an empty gameobject in the scene, which will have scale and rotation properties set to 0,0,0. Then you make a simple animation in which you modify the Y rotation with let’s say 45 degrees (door open). The animation will rotate the parent, and in that parent you can do whatever you like with your mesh collider and other things, even rotate them to make another door on a 90 degree wall. And the script logic will be the same, except you play the animation on that main parent object. You can make prefabs and so on…

yes that works like a charm thank you :slight_smile: one other thing with door animations how would i go about making a trigger say press a button door opens would that just be call animation on target object when triggered

yes, in your script you can check to see these functions.
OnMouseEnter(), OnMouseExit() and OnMouseDown() which are called on objects with colliders. So you’ll place your script on the collider object, then reference the parent of the door and the mesh, the one with animation component on it, and… on mouse enter you change the default diffuse color to green, and on exit back to its initial value (to be able to see you can interact with that object) and on mouseDown() you play the animation on the door parent. if it’s open you play close, if it’s closed you play open.

Of course! thanks yeah that has worked, you have probably saved me a week of trying to work that one out haha, thanks for your time, have it all working now because of you :slight_smile:

glad I could be of help.