Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by incitement · Aug 07, 2010 at 03:15 PM · raycastcollidertrigger

active script from distance

Hello I have a big problem. I like to make my script door active from a distance. the situation is that my door has a script to be open when i press a keyboard such us i in my case. but i like that this script be active when the player is near the door and not from the bigenning of the game. I tried a simple script with raycast but that dosn't work

this is the script

var rayCastLength = 5;

function Update() {
var hit : RaycastHit;

//check if we're colliding if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength)) { //...with a door if(hit.collider.gameObject.tag == "Port 1")

     //open the door

     if(Input.GetKeyDown("i"))
                 animation.Play("port 1");

}

}

Please Help;

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Wolfram · Aug 07, 2010 at 03:34 PM

You don't need a RayCast for that. You can use a simple distance check:

var triggerDistance:float=5;
if((door.transform.position-transform.position).sqrMagnitude<triggerDistance*triggerDistance){
    // check for key event etc.
}

(Note you should always use sqrMagnitude and the square in the comparison, instead of magnitude, due to performance reasons)

Another method would be to create a collider around your door, and only test for the key event if the player is inside that collider:

function OnTriggerStay(other:Collider){ // not sure if that's the correct function - check the manual/tutorial/examples
    // check for key event etc.
}

A note regarding your code, you always cast your ray straight forward, so you will only get a hit if you actually directly look at the door(-collider), which might be the reason your code doesn't seem to work.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by incitement · Aug 07, 2010 at 06:41 PM

I try it but it dosn't work with me. i'm new with scripting in unity so i found a problem to write a script. Thanks for your help

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Borgo · Jan 11, 2011 at 02:02 PM

I have created a script that move a door, you can alter to rotate: Some variables are in portuguese (I'm from Brazil, sorry), but I explan each one:

Person: The character that you control.

Distancia: Min distance to open door.

toWhere : To where the door would move e.g:.

Up x:0 y:2 z:0,

Left: x: -5 y:0 z:0

Misc: x:5 y:2 z:-3

velocidade: Velocity

var person : Transform; var distancia : float; var toWhere : Vector3; var velocidade : float;

private var iniPosition : Vector3; private var endPosition : Vector3;

function Start(){ iniPosition = transform.position; endPosition = iniPosition+toWhere; }

function Update () {

             var dist = Vector3.Distance(person.position, iniPosition);


             if(dist &lt; distancia){
                            transform.position.x = transform.position.x+((endPosition.x-transform.position.x)*Time.deltaTime*velocidade);
                            transform.position.y = transform.position.y+((endPosition.y-transform.position.y)*Time.deltaTime*velocidade);
                            transform.position.z = transform.position.z+((endPosition.z-transform.position.z)*Time.deltaTime*velocidade);
             }else{
                            transform.position.x = transform.position.x+((iniPosition.x-transform.position.x)*Time.deltaTime*velocidade);
                            transform.position.y = transform.position.y+((iniPosition.y-transform.position.y)*Time.deltaTime*velocidade);
                            transform.position.z = transform.position.z+((iniPosition.z-transform.position.z)*Time.deltaTime*velocidade);
             }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Raycast hits a collider and halts. 1 Answer

Raycasts hit triggers disabled - force collision for specific raycast 1 Answer

raycast ignore trigger colliders? 5 Answers

Can't click gameobject when over another trigger? 1 Answer

Why do my bullets only destroy themselves sometimes after colliding with the wall? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges