unity jumping collisions - how enemy is damaged - unity3d

I am making a platformer in Unity using unityscript.
I have a Player parent object with a character controller and various child objects. I have a similar enemy with a box collider. I'm struggling to differentiate between the collision happening when the player walks into the enemy and when the the player jumps and collides with it from above.
I've tried tagging the child objects but they don't have colliders. If I add colliders to the child objects, it messes up my character movement. I've also tried to test the position of the player:
if(col.transform.position.y >= transform.position.y){ killThyself(); }
But this doesn't work either - should I add the height of the enemy? If so how do I do that?
Any suggestions happily received.

Mmmm, I would use a boolean variable. Make it true when you press the jump button, and false once the jump has finished (when the player touches the floor). Declare the boolean as a public variable.
After that, in the OnCollision method, check the variable value.
I recommend you to read this to understand how to do that:
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Well, in your case I would assign the "otherScript" (the player script) doing this in the enemy control script:
var player : GameObject;
var playerScript: PlayerScript;
//I'm assuming "PlayerScript" is the name of the control script of your player,
//where you defined the "jump" variable.
function Start()
{
player = GameObject.FindGameObjectWithTag("Player");
//You also can use GameObject.Find("PlayerName")
playerScript = player.GetComponent(PlayerScript);
//Get the script from the player
}
function OnCollisionEnter(col : Collision)
{
if (playerScript.jump == true)
{
killThyself();
}
}
I'm finding the player on the function Start, to find it only ONCE, in the level load. Now, I assign the player script into a variable, so, when the player hit the enemy, this enemy will check if the player is jumping using his script. Remember to declare this variable as a public variable or this won't work.
Please comment if you have any problem using this method =)

I would use a second collider at the upper part of the enemy let's called it headCollider. Set isTrigger = true and maybe use a special physics material. If this headCollider is the first one to get triggered you know that the player chararacter is jumping on the enemy or is falling on it from above.
In OnTriggerEnter (Collider other) you can prepare some status variables (and maybe a timer for resetting the status). Also the current jump status like in V_Programmer's answer suggested should be useful for evaluation.
As Unity does not allow you to attach 2 colliders of the same kind, you have to use box and sphere or create an empty child and use that one.

Related

Detect collision in circle and destroy other car unity

I am working on a car multiplayer game and i want if the player 1 and player2 is in circle and the other players (player 3,4,5) are outside the circle then the outside players (player 3,4,5) destroy and the circle players (player 1,2) won.. I dont understand hows it possible
I use ontrigger but i dont understand how can I apply this
Well with the limited knowledge of your project, the best advice I can give is have a collider for that circle, make it a trigger, and make a Collision script for it where you have the following method
void OnTriggerEnter (Collider other)
{
if (other.gameObject.name(or tag) == "Whatever the tag or name you want")
{
// check here for some field or identifier for the specific instance
// of the object to see which player entered the circle
carMasterScript.DestroyEverythingExcept(other);
}
}
Once you replace the vague things here with what you have in your scripts, and possibly make some Master script so you can have a single instance that can reach and access every car/player instance, this should trigger when another trigger object(make the cars trigger rigidbodies or kinematic bodies as well) and check to see if the thing that collided is a car, then protects that instance of the car while destroying every other one

Using one scene for infinite levels

I am trying to make a game where there is a single scene, which will be used for an infinite number of levels until the player loses. All of my code is currently in a GameController script, and I have it so that when the level is completed, the player will press a key and integer for the level will be incremented, and the same scene will be loaded.
void Start()
{
gamePlaying = false;
currLevel = 0;
BeginGame();
}
void Update()
{
if (Input.GetKeyUp(KeyCode.Return))
{
currLevel++;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
I have this script and a DontDestroyOnLoad script attached to the GameController gameObject.
This currLevel variable is staying at 0 when printing to the console; however, it is updating in the inspector. What am I doing incorrectly, and is this a good way of setting up this project? Thank you!
There are some ways to implement this but I would recommend to not reload the scene but have a list or container with the objects you want to destroy and when loading the next scene you destroy these and create new. You can easly make a loading screen in this time and even better pre buffer the new scene when you notice the old one is comming to an end.
So instead of reloading the scene, destroy unnecessary objects and create the onse you need. Depending on your needs you may even reuse some as the floor for example and just change the color for your level. As said that all depents on your usecase.

Access the a child of a GameObject through a script that is assigned to it

I have a problem in making a little game in Unity 5. I have a prefab (named "Controller") and it is instantiated two times in the game. This prefab has a script attached to it, and also has 4 children of type GameObject. How can i access a child to the clone that it is instantiated? I need to change it's layer to Ignore Raycast if a button is pressed, but i don't know how to do it.
You can do it assigning your instance to a GameObject:
GameObject clon = Instantiate (Resources.Load("MyInstanceObject")) as GameObject;
clon.transform.FindChild("Mychildname").gameobject.layer = index;
or too if you know your child index:
clon.transform.GetChild(index).gameobject.layer = index;

Move forward continously with a Character Controller?

My question sums up what I'm trying to do.
I searched the internet and everyone keeps giving me the usual transform.Translate answer which doesn't work for me because I need the physics and collisions not just move the character forward.
I know how to do this with a rigidbody but my character keeps rolling, something I don't want.
If you character rolls, you could freeze some rotation constraints on the rigidbody.
Anyway here's something from the scripting reference for character controllers: https://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html
From it, I guess you can adjust for what you need.
Use a CharacterController and use its Move and SimpleMove functions. See the default character package to learn more about it, and, you could use the character motor script from that package as well.
Well First Add The Character Controller component create a new script called Move then open it and copy and paste this code
#pragma strict
function Update () {
var Controller : CharacterController = GetComponent(CharacterController);
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
Controller.SimpleMove(forward);
}
After that add the script to your player object then you are done:)

Collision detection between two character controllers

I am new to Unity and scripting. I have two players and both are using a character controller. I have done this thing
I have used onControllerColliderHit function.
I have print the name like this gameobj.name.
It will show the name of the object that it hit
But the problem is it passes through it. I want that it's not able to pass through it and it will behave just like rigid bodies have. Like there must be effect of force through which second player hit it.
Check the doc. CharacterController as is has no RigidBody attached:
The Character Controller is mainly used for third-person or
first-person player control that does not make use of Rigidbody
physics.
Basically CharacterController is only a Collisor designed for preventing compenetration between the character and objects in the scene, but doesn't work as you expect when the collision occurs between 2 CharacterControllers.
Particularly:
The Controller does not react to forces on its own and it does not
automatically push Rigidbodies away.
Like shown in OnControllerColliderHit documentation you can manually handle the collision when this occurs. For example you can push away from each other the character controller object when they collide since you have the move direction:
Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
transform.Translate (-pushDir * offset);