I have the latest version of Unity. My code seems accurate with the class, but my xThrow and yThrow do not seem to be passing through to ProcessRotation(). I can’t get any pitch with location or roll. If I try and print yThrow in the ProcessRotation function it will only show zero.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class player : MonoBehaviour
{
// order in rotation matters
// x is pitch axis, y is yaw axis, z is roll axis
[Tooltip("In ms^-1")] [SerializeField] float xSpeed = 15f; //4 meters / sec
[Tooltip("In ms^-1")] [SerializeField] float ySpeed = 15f; //4 meters / sec
[Tooltip("In m")] [SerializeField] float xRange = 4f;
[Tooltip("In m")] [SerializeField] float yRange = 3f;
[SerializeField] float positionPitchFactor = -5f;
[SerializeField] float controlPitchFactor = -20f;
[SerializeField] float positionYawFactor = 5f;
[SerializeField] float controlRollFactor = 5f;
public float xThrow, yThrow;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
ProcessTranslation();
ProcessRotation();
}
private void ProcessTranslation()
{
//can change gravity or sensitivity to adjust movement in input settings
float xThrow = CrossPlatformInputManager.GetAxis("Horizontal"); // can use "a" or "d" to throw from -1 to 1, will work for gamepad or keypads
float yThrow = CrossPlatformInputManager.GetAxis("Vertical"); // can use "s" or "w" to throw from -1 to 1, will work for gamepad or keypads
float xOffset = xThrow * xSpeed * Time.deltaTime;
float rawXPos = transform.localPosition.x + xOffset; //transform.Position would be in the world which is not what we want
float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);
transform.localPosition = new Vector3(clampedXPos, transform.localPosition.y, transform.localPosition.z);
float yOffset = yThrow * ySpeed * Time.deltaTime;
float rawYPos = transform.localPosition.y + yOffset; //transform.Position would be in the world which is not what we want
float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);
transform.localPosition = new Vector3(transform.localPosition.x, clampedYPos, transform.localPosition.z);
}
private void ProcessRotation()
{
//pitch
float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
float pitchDueToControlThrow = yThrow * controlPitchFactor;
float pitch = pitchDueToPosition + pitchDueToControlThrow;
//yaw
float yaw = transform.localPosition.x * positionYawFactor;
//roll
float roll = xThrow * controlRollFactor;
transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
}
}