Search Unity

C# simple jetpack, need help

Discussion in 'Scripting' started by Tacosaurus, Mar 5, 2015.

  1. Tacosaurus

    Tacosaurus

    Joined:
    Mar 5, 2015
    Posts:
    8
    Everything looks like it should work, but I'm getting an error on line 18. Any suggestions?
    (Sorry if there is a code input thing for the forums, I can't find one. The red line is where there's an error)

    -------------------------------------------------------Code-----------------------------------------------------------

    using UnityEngine;
    using System.Collections;

    public class Movement : MonoBehaviour {

    public float spd = 6.0f;
    public float jumpSpeed = 8.0f;
    public float grav = 20.0f;

    Vector3 moveDirection = new Vector3(0,0,0);

    // Update is called once per frame
    void Update () {
    CharacterController controller = GetComponent<CharacterController>();
    if (controller.isGrounded){
    /*We are grounded to start off, so recalculate
    move direction directly from axes*/
    moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= spd;

    if (Input.GetKey(KeyCode.LeftShift)){
    moveDirection.y = jumpSpeed;
    }
    }

    //Apply gravity
    moveDirection.y -= grav * Time.deltaTime;

    //Move the controller
    controller.Move (moveDirection * Time.deltaTime);

    }
    }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    Deleted User likes this.
  3. Tacosaurus

    Tacosaurus

    Joined:
    Mar 5, 2015
    Posts:
    8
    That's one of the problems. If I use new Vector3, it just vibrates.
     
  4. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Doesn't a character controller already account for gravity? In essence, are you not doubling its effect by adding it in yourself?

    Could be wrong but its worth a shot just commenting out that part.
     
  5. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Hmm... seems to be more stuff there too. So, whether you are grounded or not your gravity calculation is being applied, which means it could be moving your player or jetpack below the terrain or surface that it is standing on.

    So change the Vector3 to "new Vector3" like mgear suggested, but also but an if (!isGrounded) check around your gravity calculation and see if it still vibrates.
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If those public fields aren't having their values overwritten in the inspector, then you have specified a jump speed of 8 and a gravity speed of 20. That means with the jump pack firing, you have a net vertical speed of -12.

    Or at least you would, except that gravity is being multiplied by Time.deltaTime (and then you multiply the whole moveDirection vector by Time.deltaTime again afterwards). Which means that the actual strength of your gravity is going to change based on your framerate.