Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with Jumping & camera script wanted ... :DD

Discussion in 'Scripting' started by bonden55, Dec 30, 2017.

  1. bonden55

    bonden55

    Joined:
    Dec 30, 2017
    Posts:
    31
    Hello Can some one help me with my script =

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

    public class PlayerJump : MonoBehaviour {


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

    // Update is called once per frame
    void Update () {

    if(Input.GetKeyDown(KeyCode.Space))
    this.gameObject.GetComponent<Rigidbody> ().AddForce (Vector3.up * 200.0f);

    }
    }

    It works i can jump but i can press space many times and jump, jump jump jump, i only want to jump when i have hit the ground again.. right now it seams like i can fly....

    And for the other part my camera docent move with my character moving.. i want it so when my camera is facing one way and you Press "W" you walk that whey..

    Plz some one help me <3
     
  2. MickM

    MickM

    Joined:
    Nov 19, 2012
    Posts:
    166
    The reason you are flying is because you are adding that force whenever the space bar is pressed.

    What you want to do is add an impulse when space is pressed and you are grounded.

    The part in italics is the logic that is missing and can directly fit in to your conditional.
    The next step is how to figure out if you are grounded... That is implementation specific but a relatively simple implementation consists of having a 'feet' position identified (either as a transform to use the position of or a vector offset) and raycast downwards (or some other casting implementation depending on how your controller is set up)
    Another way is to have a trigger which tracks objects entering/exiting and sets a flag (although this can be a bit more difficult to avoid edge cases)


    You will quickly find that controllers can be very difficult (and there are a range of different implementations - eg. using forces, velocities, positions). Unfortunately I dont have a good tutorial handy but if you google unity first person controller tutorial you will find a good range.

    re: Camera movement, you can have a look at the unity FPS controller camera script for inspiration but the cinemachine cameras also offer an interesting way to implement your controller camera.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Please read this thread about how to post code on the forums: https://forum.unity.com/threads/using-code-tags-properly.143875/

    With regard to multiple jumps, that is a common question. Did you try to search for an answer?
    You must know when you are grounded or not, to determine if you can jump. I find that using a raycast is the best solution for this; cast down a short distance and if you make contact, you assume you're on the ground (I would skip casting onto triggers*).
    - You can try to examine the code in the standard assets third person controller, for a more detailed example, if you'd like.

    As for your camera, that can be solved by using the camera's forward as your movement direction. This however may not work directly, if your camera can move in other directions, too.
    One option can be that you rotate your player to face the direction the camera is pointing (on the Y-axis). From there, you can use your own transform.forward (which is the same as the camera).. :)

    Hope that helps.
     
  4. bonden55

    bonden55

    Joined:
    Dec 30, 2017
    Posts:
    31
    tnx for the help i got it now, just one step left and that is how i make it in codes, rly new to this and i don't know what I'm doing x) haha but tnx rly much for the help :D
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It takes time, learning, and practice to be able to do more n more with Unity / programming. :)
    If you're stuck on an issue, it's not the end of the world to try to figure it out, or even put it aside for a little while to work on something else, and go back to it later.

    However it goes for you, I hope you have a good time. Take care :)