Jump to content

Hi guys i currently have this:

/////////////////////////////////////////////

using UnityEngine;
using System.Collections;

public class Controller : MonoBehaviour {

    float speed = 4f;
    float jumpspeed = 16f;
    



    void Update()
    {
        Movement ();
    }


    void Movement()
    {
        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(Vector2.right * speed * Time.deltaTime);

        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.Translate(Vector2.left * speed * Time.deltaTime);
            
        }
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector2.up * jumpspeed * Time.deltaTime);
            
        }
    }

}

/////////////////////////////////////////////

 

Now I need this: Only be able to make

/////////////////////////////////////////////

 

        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector2.up * jumpspeed * Time.deltaTime);
            
        }

/////////////////////////////////////////////

 

if the gameObject collides with something.

I'm happy to hear from ya guys!

99 Kid. Yes. A 'Youngster'.

World famous Couchpotatoe.

Link to comment
https://linustechtips.com/topic/469108-unity-help-needed/
Share on other sites

Link to post
Share on other sites

Hi guys i currently have this:

/////////////////////////////////////////////

using UnityEngine;

using System.Collections;

public class Controller : MonoBehaviour {

    float speed = 4f;

    float jumpspeed = 16f;

    

    void Update()

    {

        Movement ();

    }

    void Movement()

    {

        if (Input.GetKey(KeyCode.D))

        {

            transform.Translate(Vector2.right * speed * Time.deltaTime);

        }

        if (Input.GetKey(KeyCode.A))

        {

            transform.Translate(Vector2.left * speed * Time.deltaTime);

            

        }

        if (Input.GetKey(KeyCode.W))

        {

            transform.Translate(Vector2.up * jumpspeed * Time.deltaTime);

            

        }

    }

}

/////////////////////////////////////////////

 

Now I need this: Only be able to make

/////////////////////////////////////////////

 

        if (Input.GetKey(KeyCode.W))

        {

            transform.Translate(Vector2.up * jumpspeed * Time.deltaTime);

            

        }

/////////////////////////////////////////////

 

if the gameObject collides with something.

I'm happy to hear from ya guys!

Wait... so what do you want help with?

GTX 980 Ti / 5820k / 16 GB DDR4 / 500 GB SSD / ATH-M50X

Link to comment
https://linustechtips.com/topic/469108-unity-help-needed/#findComment-6291820
Share on other sites

Link to post
Share on other sites

Wait... so what do you want help with?

Only be able to do transform.Translate(Vector2.up * jumpspeed * Time.deltaTime);

if the gameObject collides with something and (Input.GetKey(KeyCode.W))  

 

99 Kid. Yes. A 'Youngster'.

World famous Couchpotatoe.

Link to comment
https://linustechtips.com/topic/469108-unity-help-needed/#findComment-6291901
Share on other sites

Link to post
Share on other sites

hello,

 

I've just started working with unity, I'm no expert but for what i remember from the tutorials that I saw is that I used a boolean (grounded) (for refference see my code in the spoiler)

and with a layermask and an empty object (that has a collider attached) you can check if that empty object is touching any colliders on the specified layer. I hope this solves your problem, if not feel free to ask about anything that isn't clear.

 

Brecht

 

using UnityEngine;using System.Collections;public class Agent_O_Movement : MonoBehaviour {public bool grounded;public Rigidbody2D rb;public LayerMask groundLayer;public LayerMask Enemy;public Transform groundCheck;public float JumpForce;public int jumper;void Start () {health = 5;grounded = false;rb = GetComponent<Rigidbody2D>();}void FixedUpdate () {grounded = Physics2D.OverlapCircle (groundCheck.position, 0.2f, groundLayer); if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {// Get movement of the finger since last frameVector2 touchDeltaPosition0 = Input.GetTouch(0).deltaPosition;if (touchDeltaPosition0.y > jumper){Jump ();}public void Jump(){if (grounded == true) {rb.velocity = new Vector3 (rb.velocity.x, 10, 0);}} 

 

 

Link to comment
https://linustechtips.com/topic/469108-unity-help-needed/#findComment-6296053
Share on other sites

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

×