- Home /
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;
}
}
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;
}
}
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.
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.
The teleporter 2 and 3 aren't finished. I'm working on fixing the first, then the 2nd and 3rd.
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};
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."
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).
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?