Falling Platform using rigidbody - unity3d

I have a platform that i want to fall once the player has stepped on it. It has two box colliders. One for it's physical being and the other as a trigger. It also has a rigidbody which is kinematic by default so it doesn't fall straight away. However when the player steps on it it does nothing. Can any one tell me what's wrong with my code?
var yourObject : GameObject;
function OnTriggerEnter(Other : Collider){
if(Other.gameObject.tag == "Player"){
yourObject.rigidbody2D.isKinematic = false;
}
}

Plase check your gameobjects tag.
and also i see you use Rigidbody2D so you don't add below
var yourObject : GameObject;
function OnTriggerEnter(Other : Collider){
if(Other.gameObject.tag == "Player"){
yourObject.rigidbody2D.isKinematic = false;
}
}
You need to use this
function OnTriggerEnter2D(other: Collider2D) {
if(other.gameObject.tag == "Player"){
yourObject.rigidbody2D.isKinematic = false;
}
}
And also please check you add tag to gameobjects (I think you mean check names)
Like
function OnTriggerEnter2D(other: Collider2D) {
if(other.gameObject.name == "Player"){ //check name
yourObject.rigidbody2D.isKinematic = false;
}
}

To achieve a falling platform of sorts you do not have to make use of the IsKinematic function.
Instead, you can just turn off the Use Gravity function. This will prevent the object from falling, until another object with a rigidbody and a mass bigger then the mass of the platform touches it. As can be seen in the GIF below.

Related

Why is "Colls" an empty Array?

I try to judge a player's approach to a monster using "OverlapSphere."
But somehow it doesn't work the way I want it to.
Is there anything wrong with the code below?
bool IsPlayerNear()
{
Collider[] colls = Physics.OverlapSphere(transform.position, 3f);
foreach(Collider hit in colls)
{
if(hit.gameObject.tag == "Player")
return true;
}
return false;
}
The player character has a capsule collider and "Player" tag.
But the "colls" array contains nothing.
A frequent cause of this sort of problem is that the capsule collider is attached to a child or parent of the tagged gameobject, so maybe check that, your code looks OK to me.

How to get my overlapsphere to work?

This is my error code :
Assets/Scripts/PlayerDamage.cs(166,35): warning CS0219: The variable `hitCol' is assigned but its value is never used
I'm just not sure why it's not working properly. I thought I was using it in the input function under attackVKC. I should mention this is for a multiplayer game in unity.
Basically damage is getting done to the player casting the attack and not to the other players. I'm trying to get an aoe (area of effect) attack.
private Collider[] hitColliders;
else if (Input.GetButton ("Special") && (Input.GetButtonDown ("Fire2") && offcd == true && offccd == true)) {
attackVKC();
StartCoroutine("GlobalCooldown");
StartCoroutine("GlobalCCooldown");
}
[Client]
void attackVKC(){
hitColliders = Physics.OverlapSphere (transform.position, special.rangeVKC, mask);
{
if (GetComponent<Collider>().tag == PLAYER_TAG)
{
CmdPlayerHit(GetComponent<Collider>().name, special.damageVKC);
}
}
Debug.LogError (GetComponent<Collider>().name);
}
I actually sloved this. I wasn't looping through the array and I didn't realise getcomponent was for the gameobject attached. So that's why it was hitting the player using the attack and not other players.

[Unity][Javascript] Why are these simple js scripts not working in Unity?

I've been sort of teaching myself and sort of learning from Jimmy Vegas on youtube: https://www.youtube.com/channel/UCRMXHQ2rJ9_0CHS7mhL7erg
If you haven't seen those tutorials or don't want to look, one of the things he does is create a small script that destroys a coin when the player collider hits it, but mine isn't working. Code below (a little mis-formatted, sorry, couldn't get it to format correctly):
function OnCollisionEnter (collision : Collision) {
if(collision.gameObject.tag == "coinCollect") {
Destroy(this.gameObject);
}
}
I applied the script to a prefab and placed a bunch of coins around a little area, additionally, I made a capsule collider in a first person controller tagged "coinCollect", and ticked "Is Trigger"
Also, I'm trying to make a teleporter that teleports the first person character from one teleporter to another. Code below:
var warptarget001 : GameObject;
var warptarget002 : GameObject;
function OnTriggerEnter (col : Collider) {
if (col.gameObject.tag == "warp001") {
this.transform.position = warptarget002.position;
} else if (col.gameObject.tag == "warp002") {
this.transform.position = warptarget001.position;
}
}
I have four objects here, two warp pads and two warp targets. The two warp pads are tagged "warp001" and "warp002", respectively and the two warp targets are not assigned anything in the code, but assigned by dragging and dropping an empty object into the Serialized Field the script provides. Both pads have capsule colliders with "Is Trigger" unticked but it doesn't work either way, ticked or unticked.
Can anyone tell me what I might be doing wrong? Thank you.
The script was all correct, my problem was that my parent "FPSController" object didn't have a Rigidbody applied to it and should be the only object (as opposed to the "FirstPersonCharacter" object I had nested inside of it) that the scripts are applied to. That seemed to fix the problem.
The correct code is:
/* coincollect.cs */
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class coincollect : MonoBehaviour {
private int _score;
[SerializeField]
private Text _text;
void OnTriggerEnter ( Collider collision ){
if(collision.gameObject.tag == "coin"){
Destroy(collision.gameObject);
_score++;
_text.text = "Score: " + _score;
}
}
}
and:
/* warp.js */
var warptarget001 : GameObject;
var warptarget002 : GameObject;
function OnTriggerEnter (col : Collider) {
if (col.gameObject.tag == "warp001") {
this.transform.position = warptarget002.transform.position;
}
if (col.gameObject.tag == "warp002") {
this.transform.position = warptarget001.transform.position;
}
}
The only thing I can think of for your first problem is that it shouldn't need IsTrigger ticked. Other than that, it sounds like it should work (unless I'm missing something).
For the second problem you're having (with the warps), I don't think you can use warptarget001 by dragging and dropping objects into the fields. The reason being that what you've dragged into that field isn't the same object instance that's inworld.
You should assign their values through the code (preferably in the Start method), by using GameObject.Find("name") for example. This way, warptarget001 corresponds to the inworld gameobject.

Compare tags in UnityScript

We are building a 3D game and right now I am stuck on an issue where I need to compare multiple tags to activate or deactivate a trigger that sends the player back to the respawn position.
This is currently the code:
#pragma strict
var spawnPoint : GameObject;
function OnTriggerStay ( other : Collider )
{
if (other.tag == "Player" && other.tag != "Ball")
{
other.tag == "Player";
other.gameObject.transform.position = spawnPoint.transform.position;
}
else if ( other.tag == "Ball" && other.tag == "Player" )
{
}
}
I am uncertain how to fix this to do the following:
If the player touches the trigger without there being a ball colliding with it, the player respawns. This is to create the feeling that the particles kill you.
If the player touches the trigger when there is a ball colliding with it as well, nothing happens to the player and so the player can freely pass through.
What we want to do is that we want to push a ball over a geyser so it covers it and if the geyser is not covered and the player tries to pass over it, the player respawns.
We also tried with another code and while it allows the ball to pass, it does not allow the player to do so. Even after the ball has been placed.
#pragma strict
var spawnPoint : GameObject;
function OnTriggerStay ( other : Collider )
{
if (other.tag == "Ball")
{
other.enabled = false;
}
else if (other.tag == "Player")
{
other.gameObject.transform.position = spawnPoint.transform.position;
}
}
So, I believe your problem is, when you use that function, you are only checking the first collision. So whats happening is your getting a value of either the player or the ball, not both. You need to store all the collision so you can compare all of them. To do that you can follow the generals of this documentation. http://docs.unity3d.com/ScriptReference/Collision-contacts.html
It's talking about collisions, but the same general principle should apply.
Hope this helps!

Detect if third person camera touches cube unity3d

What I want is to destroy the cube when the third person camera touches it...but anything i have tried so far fails...
Here is the code i have tried:
#pragma strict
var other : GameObject;
function Start () {
}
function Update () {
}
function OnCollisionEnter ( collision : Collision) {
if (collision.tag == "Character")
Destroy (collision.gameObject);
}
Thanks for any suggestions!
There are two simple ways to do this. One of them is attaching a script to the character to destroy specified objects and the other one is to attach a script to the object to be destroyed on inpact with the character, but on both of those ways you NEED the Rigidbody component to be attached too.
Adding this to the object to be destroyed and tagging the character:
[RequireComponent (typeof (Rigidbody))]
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Character")
Destroy(this.gameObject);
}
OR
Adding this to the character and tagging the objects to be destroyed:
[RequireComponent (typeof (Rigidbody))]
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "ToBeDestroyed")
Destroy(col.gameObject);
}
remember: this code is in C#, you`ll need to convert to javascript if you are going to add to an existing script