Unable to correct vertical movement

Original, higher quality video (My Google Drive)

Sorry if I mess something up with this post, I don’t post to the internet often and this is my first post to the site.
edit: apparently i messed something up with google drive. i’m not too familiar with using that either. when i move up, the nose of the ship moves down. and when i move down, the nose of the ship also moves down.

As you can see, my vertical movement is off, I cannot seem to fix the odd rotation.

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

public class Player : MonoBehaviour {

    [SerializeField] float xSpeed;
    [SerializeField] float xClampRange;
    [SerializeField] float ySpeed;
    [SerializeField] float yClampRange;

    [SerializeField] float positionPitchFactor = -5f;
    [SerializeField] float controlPitchFactor = -5f;
    [SerializeField] float positionYawFactor = 5f;
    [SerializeField] float controlRollFactor = -20f;

    float xThrow;
    float yThrow;
    

    // Use this for initialization
    void Start () {
        
    }
	
	// Update is called once per frame
	void Update ()
    {
        xYMovement();
        ProcessRotation();
        
    }

    private void ProcessRotation()
    {
        float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
        float pitchDueToControlThrow = yThrow * controlPitchFactor;

        float pitch = pitchDueToPosition * pitchDueToControlThrow;
        
        float yaw = transform.localPosition.x * positionYawFactor;

        float roll = xThrow * controlRollFactor;

        transform.localRotation = Quaternion.Euler(pitch, yaw, roll);

        print(pitchDueToControlThrow);
    }

    private void xYMovement()
    {
        xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
        float xOffset = xThrow * xSpeed * Time.deltaTime;


        float rawXPos = transform.localPosition.x + xOffset;
        float clampedXPos = Mathf.Clamp(rawXPos, -xClampRange, xClampRange);

        yThrow = CrossPlatformInputManager.GetAxis("Vertical");
        float yOffset = yThrow * ySpeed * Time.deltaTime;

        float rawYPos = transform.localPosition.y + yOffset;
        float clampedYPos = Mathf.Clamp(rawYPos, -yClampRange, yClampRange);

        transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
    }
}

Hello Matthew, welcome to the community! :slight_smile:

I have resolved your video issue, the forum will try too embed your linked content, but it doesn’t work very well without a specific file URL. I have re-recorded your video at a lower resolution and uploaded it into your original post, for completeness I have linked to your Google Drive beneath for the original. The maximum upload size for files on the forum is 10mb, so for video, it’s often best to upload them to YouTube, you can then just paste the URL into your posts and they will be embedded correctly.

I have also applied code formatting to your copy/pasted code, please see the link below.

Regarding your issue, I’ve compared your code to that for the Project Changes linked from the Resources on this lecture of the course, there are some differences, one of the most noticeable is the difference in values you have for the controlPitchFactor;

Your code;

[SerializeField] float controlPitchFactor = -5f;

Course code;

[SerializeField] float controlPitchFactor = -20f;

Appreciating of course you may have changed these values in the Inspector.

In addition, your third line of the ProcessRotation method;

float pitch = pitchDueToPosition * pitchDueToControlThrow;

Course code;

float pitch = pitchDueToPosition + pitchDueToControlThrow;

It might be worth going back through this lecture and just comparing your code to that in the course to be sure there are no other differences.

Hope this helps :slight_smile:


See also;

1 Like

Thank you so much for fixing both my problems! I cannot believe I turned a + into a * and kept looking over that dissimilarity for 2 hours. This has taught me a valuable lesson. I really appreciate your help!

2 Likes

Hi Matthew,

You are more than welcome, and I am glad you can move forward again.

I will look forward to seeing your version of Argon Assault in due course if you choose to share it with us. :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms