Here’s the entire code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerControl : MonoBehaviour
{
[Header("General")] //Used to give a heading to a group of variables in the inspector. Useful for organization.
[Tooltip("In ms^-1")] [SerializeField] float xSpeed = 10f;
[Tooltip("In ms^-1")] [SerializeField] float ySpeed = 10f;
[Tooltip("In m (Position to clamp movement)")] [SerializeField] float xRange = 5f;
[Tooltip("In m (Position to clamp movement)")] [SerializeField] float yRange = 3.5f;
[Tooltip("Array of both left and right guns")] [SerializeField] GameObject[] guns;
[Header("Screen-Position based")]
[SerializeField] float positionPitchFactor = -9f;
[SerializeField] float positionYawFactor = 10f;
[Header("Control-Throw based")]
[SerializeField] float controlPitchFactor = -20f;
[SerializeField] float controlRollFactor = -30f;
float xThrow, yThrow;
bool controlEnabled = true;
void Update() {
if(controlEnabled == true) {
ProcessMovement();
ProcessRotation();
ProcessFiring();
}
}
void OnPlayerDeath() { //Called by string reference (Rename the string in CollisionHandler if you change the name here)
controlEnabled = false;
}
void ProcessMovement() {
xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
yThrow = CrossPlatformInputManager.GetAxis("Vertical");
float xOffset = xThrow * xSpeed * Time.deltaTime;
float yOffset = yThrow * ySpeed * Time.deltaTime;
float RawPosx = transform.localPosition.x + xOffset;
float RawPosy = transform.localPosition.y + yOffset;
float clampedXPos = Mathf.Clamp(RawPosx, -xRange, xRange);
float clampedYPos = Mathf.Clamp(RawPosy, -yRange, yRange);
transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
}
void ProcessRotation() {
float pitchDueToPos = transform.localPosition.y * positionPitchFactor;
float pitchDueToRot = yThrow * controlPitchFactor;
float Pitch = pitchDueToPos + pitchDueToRot;
float Yaw = transform.localPosition.x * positionYawFactor;
float Roll = xThrow * controlRollFactor;
transform.localRotation = Quaternion.Euler(Pitch, Yaw, Roll);
}
void ProcessFiring() {
if(CrossPlatformInputManager.GetButton("Fire")) {
ActivateGuns();
}
else {
DeactivateGuns();
}
}
void ActivateGuns() {
foreach (GameObject gun in guns) {
gun.SetActive(true);
}
}
void DeactivateGuns() {
foreach (GameObject gun in guns) {
gun.SetActive(false);
}
}
}
And here’s a video clip of what I mean by the position doesn’t change: https://mega.nz/file/E4UHhYRL#hdVJTIvlEmukK0YGcew8eTAJW_1MiyOl5YSzTWHbOlY