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
1
Question by icebird1942 · Aug 13, 2013 at 07:54 PM · collisionfirst-person-controller

Do something when player touches gameObject

I'm trying to make a script (in Javascript) that when the player (First Person) touches a certain object, that object translates another object downward (right now I'm trying to replicate the COD: WAW Zombies linking the teleporters. Code so far: This script is on the teleporters. I plan to have 3 scripts so the touched var works seperately, but making one script for all 3 teleporters would be nice. i have no idea what the tag part is so that might be the problem. I named First Person Controller to Player, but that didn't help.

 #pragma strict
 
 var door : Transform;
 var touched : boolean = false;
 function OnCollisionEnter (collision:Collision) {
 if (collision.gameObject.tag.Equals("Player")&& touched==false){
 door.transform.Translate(0,-1.3,0);
 touched=true;
 }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by YoungDeveloper · Aug 14, 2013 at 01:23 AM

Hi, OnCollisionEnter won't work on FPS player, you must use OnControllerColliderHit, and it's better to use one collision checker on player itself than on teleport. And It's hit.collider.gameObject.tag not hit.gameObject.tag Attach this script to player, not door teleporter. And tag to the door-teleportter "teleportStart". And attach an empty game object to teleportTo where you want to teleport to.

     #pragma strict
      
     var teleportTo : Transform;

     function OnControllerColliderHit (hit : ControllerColliderHit) {
     if (hit.collider.gameObject.tag == "teleportStart"){
        transform.position = teleportTo.position;
     }
     }
Comment
Add comment · Show 12 · 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 icebird1942 · Aug 14, 2013 at 05:25 PM 0
Share

Thanks! It worked!

avatar image YoungDeveloper · Aug 14, 2013 at 06:56 PM 0
Share

Just posting for the record.

Step 1. Create simple cube, and tag it "teleportPad". Place it somewhere on ground.

Step 2. Create C# script called "Teleport", and paste in this code. You can translate it later in Unityscript.

 using UnityEngine;

 using System.Collections;

 public class Teleport : $$anonymous$$onoBehaviour {
     
     public Transform destination;
     
     void OnControllerColliderHit(ControllerColliderHit col) {
         if(col.collider.gameObject.tag == "teleportPad"){
             transform.position = destination.position;
         }
     }
 }

Step 3. Save the script and attach it to player, (not camera).

Step 4. Create new empty game object, place it somewhere 4 meters above the ground so you wouldn't fall through.

Step 5. Drag and drop the created empty game object (created in step 4) onto "Destination", located in "Teleport" script on your player.

Play it.

avatar image icebird1942 · Aug 15, 2013 at 04:12 PM 0
Share

I use JS so some of that script is unknown, but I have it figured out. $$anonymous$$y new and improved code:

 #pragma strict
 var teleportTo : Transform;
 var linked1 : boolean = false;
 var linked2 : boolean = false;
 var linked3 : boolean = false;
 var timer1 : float = 0;
 var timer2 : float = 0;
 var timer3 : float = 0;
 var guitext : GUIText;
 var guitext2 : GUIText;
 var guitext3 : GUIText;
 var door : Transform;
 function Update(){
 if (timer1 > 0){
 timer1 -= Time.deltaTime;
 guitext.guiText.text = timer1.ToString("F0");
 }
 timer2 -= Time.deltaTime;
 timer3 -= Time.deltaTime;
 
 if (timer2 > 0){
 guitext2.guiText.text = timer2.ToString("F0");
 }
 if (timer3 > 0){
 guitext3.guiText.text = timer3.ToString("F0");
 }
 }
     function OnControllerColliderHit (hit : ControllerColliderHit) {
     if (hit.collider.gameObject.tag == "Teleporter1"){
     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.E) && linked1 == false){
     Debug.Log("Pressed E");
        timer1 = 10;
 }
     if (hit.collider.gameObject.tag == "Teleporter2"){
     Debug.Log("Teleported2!");
        timer2 = 10;
        timer2 -= Time.deltaTime;
        if (timer2 > 0){
 guitext.guiText.text = timer2.ToString("F0");
 }
     if (hit.collider.gameObject.tag == "Teleporter3"){
     Debug.Log("Teleported3!");
        timer3 = 10;
        timer3 -= Time.deltaTime;
        if (timer3 > 0){
 guitext.guiText.text = timer3.ToString("F0");
 }
 door.transform.Translate(0,-1.34,0);
        }
            if (hit.collider.gameObject.tag == "Teleporter1" && timer1 <= 0){
     Debug.Log("Teleported1!");
        timer1 = 10;
        transform.position = teleportTo.position;
        transform.rotation = teleportTo.rotation;
 door.transform.Translate(0,-1.34,0);
        }
            if (hit.collider.gameObject.tag == "Teleporter3" && timer3 <= 0){
     Debug.Log("Teleported3!");
        timer3 = 10;
        transform.position = teleportTo.position;
        transform.rotation = teleportTo.rotation;
 door.transform.Translate(0,-1.34,0);
        }
            if (hit.collider.gameObject.tag == "Teleporter2" && timer2 <= 0){
     Debug.Log("Teleported2!");
        timer2 = 10;
        transform.position = teleportTo.position;
        transform.rotation = teleportTo.rotation;
 door.transform.Translate(0,-1.34,0);
     }
     if (hit.collider.gameObject.name == "$$anonymous$$ainTeleporter"){
     Debug.Log("Teleporter 1 linked!");
     linked1 = true;
     }
     if (hit.collider.gameObject.tag == "$$anonymous$$ainTeleporter" && Input.Get$$anonymous$$ey($$anonymous$$eyCode.E) && timer2 > 0){
     Debug.Log("Teleporter 2 linked!");
     linked2 = true;
     }
     if (hit.collider.gameObject.tag == "$$anonymous$$ainTeleporter" && Input.Get$$anonymous$$ey($$anonymous$$eyCode.E) && timer3 > 0){
     Debug.Log("Teleporter 3 linked!");
     linked3 = true;
     }
     }
 }
 }

The only problem I'm having now is that I can't link to the mainteleporter. I press E and the timer is above 0, but still nothing.

avatar image icebird1942 · Aug 15, 2013 at 04:13 PM 0
Share

The teleporter 2 and 3 aren't finished. I'm working on fixing the first, then the 2nd and 3rd.

avatar image YoungDeveloper · Aug 15, 2013 at 05:34 PM 0
Share

Hey, there's actually a lot of code copying here which is bad. You could create much smaller script using enum. All teleport stuff should be saved on teleport itself, so it would be processed only if you access it.

 public enum type {Teleport1,Teleport2,Teleport3};
Show more comments
avatar image
0

Answer by getyour411 · Aug 13, 2013 at 07:59 PM

Below the "name" of a GameObject in the Editor is a dropdown for Tag, you need to set the "Player" tag.

Also, do you have a non-kinematic rigidbody on either of the touching objects?

http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html

"Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached."

Comment
Add comment · Show 2 · 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 icebird1942 · Aug 14, 2013 at 01:08 AM 0
Share

I changed the tag, added a non-kinematic rigidbody on the teleporter pad and player, and it still isn't working. Do I need a collision detection script on my player too or just the one object. I added a print command in the if statement to see if the transform was the problem and nothing showed up (I know where the print shows up so it's not me).

avatar image icebird1942 · Aug 14, 2013 at 01:24 AM 0
Share

I know it's something with the First Person controller and the pad. I tested in a new scene a cube hitting the floor and used Debug.Log for colliding with the floor. It worked when the tag is set to Player. What's the problem with the FPC?

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

collission not triggered when i touch object.. 1 Answer

First person controller prefab collision 1 Answer

How should I make the projectiles in a fps go straight to the middle of a screen? also, collision problems 1 Answer

Pushing First Person Controller 0 Answers

How can i get a color from a cube a make it ToString? 2 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