1

I'm totally new with Unity (and game development in general). I followed the great simple tutorial Survival Shooter and I have one question: in this tutorial, we add a Y constraint position to the rigidbody's character plus we set the drag value and the angular drag value to infinite. How can we make the character jump since those settings prevent the character from moving to the Y axis?

If someone can give me an hand on that please...

Thanks a lot!

Vaibhav Mule
  • 5,016
  • 4
  • 35
  • 52

3 Answers3

0

Why exactly do you add the constraint on the Y axis? You could remove it, and then just add gravity that will make your player stick to the ground. After that just apply a force, or just a simple translation going upwards by a set speed, to make the player jump and then wait for gravity to bring him back down.

Zee
  • 824
  • 11
  • 25
  • I tried to remove the Y constraint, but because the drag & the angular drag value are set to infinite, I cannot apply any upward forces. A solution would be to set a value for the drag & angular drag and remove the Y constraint when pressing "Jump" button, and then reset the values when grounded, but not sure to be the best solution... – user2330043 Apr 16 '15 at 10:15
  • Well the question here is why are you adding all those constraints? Is there any particular reason why you need this strict set of properties on your player? – Zee Apr 16 '15 at 14:22
0

This is what I would do to jump.
P.S. you need to remove the constraints on the Y axis on the vector

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour
{
    public Vector3 force;
    public Rigidbody rb;


    // Use this for initialization
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.Space) && transform.position.y == 0) //Enter your y axix where ground is located or try to learn a little more about raycasting ill just use 0 for an example)
        {
            rb.AddForce(force);//Makes you jump up when you hold the space button down line line 19 will do so that you only can jump when you are on the ground.  

        } if (Input.GetKeyUp(KeyCode.Space))
        {
            rb.AddForce(-force); //When you realase the force gets inverted and you come back to ground 
        }
    }

}
illright
  • 3,991
  • 2
  • 29
  • 54
CubeCrafter360
  • 192
  • 1
  • 13
-1

I would do this instead edit the code over this post.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Just : MonoBehaviour {

    public Vector3 force;
    public Rigidbody rb;
    bool isGrounded;

  
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true) //Rember to got to your "Ground" object and tag it as Ground else this would not work 
        {
            rb.AddForce(force);
        }       
    }
     void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Ground")
        {
            isGrounded = true;
        }
    }
     void OnCollisionExit(Collision collision)
    {
        isGrounded = false;
    }

}

You need to assign your ground object a tag called Ground, You need to make your own tag called Ground its not that hard tho you click your object and top left of the inspector there's the tag and then u just make a new tag called Ground. And also plz rember to assign the other values on your player object.

Community
  • 1
  • 1
CubeCrafter360
  • 192
  • 1
  • 13