- Home /
Question by
midgear · Apr 05, 2013 at 09:42 PM ·
c#doorvector3.distance
Adding Vector3.Distance to door script.
okay I made a simple little c# script to open and close doors, the only problem is I want to make it so the player can only open/close said door when she is close to it (right now you can open/close it from across the room). I know I need to add Vector3.Distance to the script, but I can't seem to get it working.
here is the script:
using UnityEngine;
using System.Collections;
public class OpenDoor : MonoBehaviour
{
bool open = false;
AudioClip openSound;
AudioClip closeSound;
void OnMouseDown () {
if (open) {
Debug.Log ("Close");
rigidbody.AddForce(transform.forward * 50);
rigidbody.useGravity = true;
open = false;
if (closeSound)
audio.PlayOneShot(closeSound);
}
else {
Debug.Log ("Open");
rigidbody.AddForce(-transform.forward * 50);
rigidbody.useGravity = true;
open = true;
if (openSound)
audio.PlayOneShot(openSound);
}
}
}
Comment
Answer by asasababoon · Apr 06, 2013 at 12:04 AM
void OnMouseDown ()
{
float dist = Vector3.Distance(door.transform.position, player.transform.position);
if (dist < 2)
{
//do open and closing
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Opening door with the same key? 1 Answer
Pausing Door Corountine During Collision 0 Answers