How to make the ship jump

Hi
I have make the ship turn left and right, up and down sucessfully as the lecture instructed . However I just wonder how to make the ship jump in the game . By now , all I do is to multiply the offset with a float variable “force” everytime player press jump button . However , I think that is not good solution
Firstly, if I press jump button (space button when I am on PC), the ship doesnt jump. It only jump when I press W+Space . Secondly, the way the ship jump look not natural. The ship jump very fast and doesnt look natural. Thirdly, player always jump too high up, out of camera’s vision.
Below is my code attached to the ship . Move() is the method I am using to control the ship turn left, right, up, down and jump.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class MovementControl : MonoBehaviour {

[Tooltip("Move speed of the character to move, unit: M/s")] [SerializeField] 
private float moveSpeed = 8; 
[Tooltip("Enable this checkbox, will enable pitch system in the object")][SerializeField] 
private bool PitchEnable = false;
[Tooltip("Enable this checkbox, will enable Yaw system in the object")][SerializeField] 
private bool YawEnable = false;
[Tooltip("Enable this checkbox, will enable Roll system in the object")][SerializeField] 
private bool RollEnable = false;
[Tooltip("This value will be the max distantce of the model move from original location in x axis")][SerializeField] 
private float MaxXMovement = 3.23f;
[Tooltip("This value will be the max distantce of the model move from original location in Y axis")][SerializeField] 
private float MaxYMovement = 2.5f;
[Tooltip("How much character can pitch dur to its position changes")][SerializeField] 
private float positionPitchFactor = -5f; 
[Tooltip("How much character can pitch dur to player's control")][SerializeField] 
private float controlPitchFactor = -20f; 
[Tooltip("How much chararacter can yaw dur to its left and right movement")][SerializeField] 
private float positionYawFactor = 5f; 
[Tooltip("How much chararacter can roll dur to its left and right movement")][SerializeField] 
private float controlRollFactor = -30f; //how much chararacter can roll

//---------Zeting: Question  this is how fast can jump or how high it can jump?
[Tooltip("How high chararacter can jump ")][SerializeField]
private float force = 100f; //how fast chararacter can jump

[Tooltip("Enable this checkbox, will enable jump system in the object")][SerializeField] 
private bool jump = false;

private float xThrow;
private float yThrow;
private Rigidbody rigidbodyPlayer;

// Use this for initialization
void Start () {
	this.setupControl();
    rigidbodyPlayer = GetComponentInParent<Rigidbody>();
}

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

	Move();// move the character left right up down
    rotate(); //  rotate the chatacter whey moving
}


/// <summary>
/// This method will check the Checkbox of yaw, pitch, roll
/// </summary>
private void setupControl()
{
	this.positionPitchFactor=(this.PitchEnable)?this.positionPitchFactor:0;
	this.controlPitchFactor=(this.PitchEnable)?this.controlPitchFactor:0;
	this.controlRollFactor=(this.RollEnable)?this.controlRollFactor:0;
	this.positionYawFactor=(this.YawEnable)?this.positionYawFactor:0;
}

/// <summary>
/// this method will will control yaw, pitch, and roll, combine them to make the rotation system
/// </summary>
private void rotate()
{
    float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;//different position when you move in Y asix

    float pitchDueToControlThrow = yThrow * controlPitchFactor; // y Throw is the joysick (if keyboard is a line) how far from the origin
                                                                //  point in y asix

    float pitch = (pitchDueToPosition + pitchDueToControlThrow)/1.5f;// pitch can be calculate, 1.5 is changable, higher value more rotate
    
    float yaw = transform.localPosition.x * positionYawFactor;//different yaw when you move in x asix
    float roll = xThrow * controlRollFactor;// roll with your joysick

    transform.localRotation = Quaternion.Euler(pitch, yaw, roll);// apply the change to che character
}

/// <summary>
/// this method will get input from CrossPlatformInputManager to move the attached object
/// </summary>
private void Move()
{
    xThrow = CrossPlatformInputManager.GetAxis("Horizontal");//get data from CrossPlatformInputManager
    yThrow = CrossPlatformInputManager.GetAxis("Vertical");
   
    float xOffSet = xThrow * moveSpeed * Time.deltaTime;
    float yOffSet = yThrow * moveSpeed * Time.deltaTime;
	
    float rowY = Mathf.Clamp(transform.localPosition.y + yOffSet, -MaxYMovement, MaxYMovement);//limited the x y way can go
    float rowX = Mathf.Clamp(transform.localPosition.x + xOffSet, -MaxXMovement, MaxXMovement);//limited the x y way can go
    if (CrossPlatformInputManager.GetButton("Jump"))
    {

        Debug.Log("jump");
        rowY = Mathf.Clamp(transform.localPosition.y + yOffSet*force, -MaxYMovement, MaxYMovement);
    }

    transform.localPosition = new Vector3(rowX, rowY, transform.localPosition.z);
}

Please help me with this . Sorry for my bad English, thank you in advance.

Hi Minh,

I think firstly you should probably decide how you want the jump to actually appear, for example, is it just going to be a sudden vertical lift, or is it going to be more like the current vertical movement, but faster, or something else.

Currently you are only making changes to your Move method, where-as in the original section code there was also the ProcessRotation as well which was used to give the pitch effect (nose of the ship up / down etc).

Finally, it might be worth waiting until you’ve got to the end of this section before implementing more changes/features, I mention this only because the use of Timeline is coming up and I don’t think you have got that far yet in this section (I could be wrong). If this requires any changes to the current approach of player movement then what you implement now may need changing later on.

Let me know your thoughts.


Updated Sun Sep 09 2018 14:31

Had a thought earlier regarding your jumping query. One of the things which makes what you want to do, at least in code, more challenging is that there are already two things controlling the position of the player ship, you have the WaypointProgressTracker.cs and also the PlayerController.cs. It occurred to me that rather than trying to incept the movements yourself with code, you could just use animation.

I test this, rather quickly I should add.

  • Add a member variable to PlayeController.cs to hold a reference to the Animator component

    Animator animator;
    
  • Now lets get a reference to the component in the Start method;

    private void Start()
    {
        animator = gameObject.GetComponent<Animator>();
    }
    
  • We can add another method in the Update method to handle the player input;

    private void Update()
    {
        if (isControlEnabled)
        {
            ProcessTranslation();
            ProcessRotation();
            ProcessFiring();
            ProcessJumping();
        }
        else
        {
            Debug.Log("You're dead!");
        }
    }
    
  • Add the ProcessJumping method too;

    private void ProcessJumping()
    {
        if (CrossPlatformInputManager.GetButton("Jump"))
        {
            animator.ResetTrigger("isJumping");
            animator.SetTrigger("isJumping");    // string reference, be wary
        }
    }
    
  • Save the script

  • Add an Animator component the PlayerShip GameObject in your Hierarchy

  • Add a new Clip in the Animation view, I named it Jump

  • Add the Position property of the Transform to the animation.

  • Insert a Key at about 0:20 and then set the Y component of the Position to a value of 3

  • In the Animator view, add a new State, I named mine Flight, this is effectively the idle state

  • Add another State, I named mine Jump

  • Create transitions in both directions between Flight and Jump

  • Select the Jump State and set Motion to the Jump clip (you can use the circle selector next to the field)

  • In the Animator, create a new Parameter, I named mine isJumping and set it as a Trigger

  • Select the transition from Flight to Jump and add a Conditon, select isJumping

  • Go to Edit / Project Settings / Input and set “Jump” to the key of your choice, note, by default it will be Space which also fires by default, I set mine to J just for testing.

  • Run the game.

You can now refine how the player ship jumps via the Animation, rather than trying to find ways to fine tune it in code.

Note, I set this at 0:20 rather than exactly in the middle of the animation clip to get more of a quick snap effect moving upwards before moving back down more slowly… you could also add the Rotation property of the Transform in the Animation clip and give the ship a little bit of pitch up and down as it jumps which may look quite nice.

Thank Rob, You are right I have not finished timeline lecture, I will continue. Still cant get the ship to jump yet however what you write here give me the direction and hope ,which make me so excited . I was so frustrated yesterday cause I cant get the ship to jump and no clue what to do next, although I did google a lot.

1 Like

You’re welcome.

I have, unfortunately, found an issue with the solution I provide above, whilst the ship now jumps, it doesn’t move across and up the screen when you press the keys. It does rotate, so I suspect the values in the animation are now overriding the values from code.

Still, something to play with :slight_smile:


Updated Sun Sep 09 2018 16:50

I created another GameObject under the camera, this one has the PlayerController.cs script on it, but nothing else.

image

The orginal PlayerShip is now a child GameObject of that. I tweaked the;

private void Start()
{
    animator = gameObject.GetComponent<Animator>();
}

to

private void Start()
{
    animator = gameObject.GetComponentInChildren<Animator>();
}

Still rough around the edges, but it does now allow for movement with the keys and the animation on the child GameObject.

1 Like

Privacy & Terms