[SOLVED] My spaceship pitch rotation is not working like into video lesson

I have Unity 2018.4.15f1 Personal. The code and settings value are identical to the lesson but my spaceship and script is not working like into the lesson. I have reset the script and the settings values of the pitching is not working even if i change the values, is doing nothing. Left, right, up and down is working well but the rotation and pitching are not working, the values is inverse (if + i must set - etc). And i have this message from console : Assets\Player.cs(18,11): warning CS0649: Field ‘Player.xThrow’ is never assigned to, and will always have its default value 0.

Why and what to do ?.

Here is the code and settings:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class Player : MonoBehaviour
{

[Tooltip("In ms^-1")] [SerializeField] float speed = 20f; // in metrii/secunda
[Tooltip("In m")] [SerializeField] float xRange = 5f;
[Tooltip("In m")] [SerializeField] float yRange = 3f;

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

float xThrow, yThrow;

void Start()
{
    
}

void Update ()
{
    ProcessTranslation();
    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);
}

void ProcessTranslation()
{
    float xThrow = CrossPlatformInputManager.GetAxis("Horizontal");//horizontalThrow
    float yThrow = CrossPlatformInputManager.GetAxis("Vertical");

    float xOffset = xThrow * speed * Time.deltaTime;
    float yOffset = yThrow * speed * Time.deltaTime;

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

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

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

}

The controlPitchFactor and controlRollFactor dosent work at all, i can change the values but do nothing. I think that is a variable problem that is not recognise by the editor.

Hi Micu,

Welcome to our community! :slight_smile:

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Have you rewatched the lecture at least one more time?

Yes and cant find the eroor or problem. My code seems to be identical.

What is the expected behaviour? And what happens instead?

Bear in mind that the Inspector overrides the values at the top of your code. Maybe you could tweak the values in your Inspector to see if the result becomes better.

Your code is almost identical.
Look at xThrow and yThrow. You define them as global members at the start, but you also define local variables with the same names at ProcessTranslation(). What it does, it updates only the local variables you create every iteration (of update()->ProcessTranslation()) instead of updating the global members.

Try to remove the float declerations from xThrow and yThrow in ProcessTranslation()

float xThrow, yThrow;

void Start()
{
    
}

void Update ()
{
    ProcessTranslation();
    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);
}

void ProcessTranslation()
{
    xThrow = CrossPlatformInputManager.GetAxis("Horizontal");//horizontalThrow
    yThrow = CrossPlatformInputManager.GetAxis("Vertical");

    float xOffset = xThrow * speed * Time.deltaTime;
    float yOffset = yThrow * speed * Time.deltaTime;

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

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

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

Tell me if that helped.

Yea, that was the problem thank you much :slight_smile: now is work fine

Happy it helped :smile:
You can now add [SOLVED] to your title’s start :wink:

Thanks a lot for helping Micu, Tom. :slight_smile:

He can mark a solution here, so the [Solved] tag in the title is not necessary here.


See also:

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

Privacy & Terms