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.