Jump to content

I need to execute this: transform.Translate(Vector2.up * jumpspeed * Time.deltaTime);

if: collisionObject.gameObject.tag == "JUMP" and Input.GetKey (KeyCode.W)

 

 

So how can i do this?

I thought like this:

if (collisionObject.gameObject.tag == "JUMP" && Input.GetKey (KeyCode.W))

{

 

}

 

But that doesnt work. :(

 

 

99 Kid. Yes. A 'Youngster'.

World famous Couchpotatoe.

Link to comment
https://linustechtips.com/topic/469123-how-can-i-do-this-unity2d/
Share on other sites

Link to post
Share on other sites

If it's a collision, shouldn't it just be on its own? You set a trigger for the collider and if that trigger receives a collision, it will do what you want or am I missing something here?

Well both of these have to be "true": collisionObject.gameObject.tag == "JUMP" and Input.GetKey (KeyCode.W)

to execute this: transform.Translate(Vector2.up * jumpspeed * Time.deltaTime);

99 Kid. Yes. A 'Youngster'.

World famous Couchpotatoe.

Link to post
Share on other sites

You should tell more what the scenario is. What are you trying to do and where do you put the script in.

Are you trying to set a trampoline-like block with tag "JUMP", where if the player step on it, they can jump by pressing "W" key?

 

To make it work, that if statement must be put on the Update() loop. and collision object should be checked first that it isn't null.

Link to post
Share on other sites

Yeah, the question is vague. What does your w do? Your character alone will trigger the collision so you don't have to register the key press.

I'm assuming your w = walk forward and if what VZX's saying is true that you want to make a trampoline-like effect, you should set a collider with a trigger for your trampoline then have your character tagged with anything you want then register the collision like this:

 

void OnTriggerEnter(Collider other)
{
if(other.tag == "your character tag")
{
do what you want to happen
}
}
 
Edit: I've just read that this was 2d so I don't know what your w does, care to explain?
 
Edit: Okay sorry I'm just too used to 3d I guess what you wanted was w to jump when you're on the area of the collider, then I guess what you should do is:
 
void OnTriggerEnter(Collider other)
{
if(other.tag == "your character tag" && keypress code)
{
do what you want to happen
}
}
Link to post
Share on other sites

 

Yeah, the question is vague. What does your w do? Your character alone will trigger the collision so you don't have to register the key press.

I'm assuming your w = walk forward and if what VZX's saying is true that you want to make a trampoline-like effect, you should set a collider with a trigger for your trampoline then have your character tagged with anything you want then register the collision like this:

 

void OnTriggerEnter(Collider other)
{
if(other.tag == "your character tag")
{
do what you want to happen
}
}
 
Edit: I've just read that this was 2d so I don't know what your w does, care to explain?

 

Basically

  1.   public void OnTriggerEnter2D(Collider2D collisionObject)
  2.     {
  3.         Debug.Log("Trigger Hit");
  4.         if (collisionObject.gameObject.tag == "h" && Input.GetKey(KeyCode.W))
  5.         {
  6.             transform.Translate(Vector2.up * jumpspeed * Time.deltaTime);
  7.         }
  8.     }
But that doesnt work. So everything i want to do:
 
If the player is on the object tagged "h" AND you press the button W it should jump.

99 Kid. Yes. A 'Youngster'.

World famous Couchpotatoe.

Link to post
Share on other sites

 

Basically

  1.   public void OnTriggerEnter2D(Collider2D collisionObject)
  2.     {
  3.         Debug.Log("Trigger Hit");
  4.         if (collisionObject.gameObject.tag == "h" && Input.GetKey(KeyCode.W))
  5.         {
  6.             transform.Translate(Vector2.up * jumpspeed * Time.deltaTime);
  7.         }
  8.     }
But that doesnt work. So everything i want to do:
 
If the player is on the object tagged "h" AND you press the button W it should jump.

 

 

Did you attach that script on the collider?

Link to post
Share on other sites

no to the player

 

Put it on the collider, it's the collider's trigger you're referring to with that code so if the collider's trigger is entered, do what you want to happen. But be sure to refer to what gameobject you're applying your transform (in your case, the player) in your code.

Link to post
Share on other sites

This should work :

using UnityEngine;using System.Collections;public class Jump : MonoBehaviour {public CharacterController player;public float jumpspeed = 15.0f;string status = "";void Update () {if (Input.GetKeyDown("w") && status == "inside"){player.transform.Translate(Vector2.up * jumpspeed * Time.deltaTime);}}void OnTriggerEnter(){status = "inside";}void OnTriggerExit(){status = "";}}

Reminders:

1. Create a C# script named "Jump" and put it on your game object that has the collider.

2. Copy the code above and put it inside the Jump script.

3. Add a reference to the character controller in Unity by going to your game object (that has the collider) and under the Jump script, select the jump variable and reference your character controller.

4. You should probably add another requirement for the if to only allow the jump if the position of the character is on the land to prevent the user on spamming jump while on air.

Link to post
Share on other sites

 

Basically

  1.   public void OnTriggerEnter2D(Collider2D collisionObject)
  2.     {
  3.         Debug.Log("Trigger Hit");
  4.         if (collisionObject.gameObject.tag == "h" && Input.GetKey(KeyCode.W))
  5.         {
  6.             transform.Translate(Vector2.up * jumpspeed * Time.deltaTime);
  7.         }
  8.     }
But that doesnt work. So everything i want to do:
 
If the player is on the object tagged "h" AND you press the button W it should jump.

 

 

That should work if you put it on player.

Did you see the "Trigger Hit" printed on the console?

If not, it's likely that the object tagged "h" have the "IsTrigger" checkbox unchecked. which means the object acts like a Collider2D ( so it can interact with another object with Collider2D), hence the OnTriggerEnter2D doesn't get called. OnTriggerEnter2D() will only get called if the object has the "IsTrigger" checkbox checked.

 

If you want some kind of physics interaction on the object tagged "h", you must put it on OnCollisionEnter2D() instead.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×