Can't move camera parent from child script with camera on timeline movement
Trying to move a parent which is the main camera using transform.parent. I've muted the unity timeline to see if it was over riding movement and couldn't. The reason I'm doing this is I have a rail shooter setup in which the player childed to the camera has a script(shown below) that handles rotation and translation across screen with the timeline moving the camera rig to progress the level. I'm wondering if there is a better way to do this as well and am open to a better structure for enabling this.
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput;
public class Player : MonoBehaviour {
[Header("Movement")]
[Tooltip("in ms^-1")][SerializeField] float xSpeed = 4f;
[Tooltip("max player movement amount")][SerializeField] float xRange;
[Tooltip("max player movement amount")] [SerializeField] float yRange = 10f;
[SerializeField] float positionPitchFactor;
[SerializeField] float controlPitchFactor;
[SerializeField] float positionYawFactor = -5f;
[SerializeField] float controlRollFactor;
[SerializeField] float controlYawFactor;
[Header("Weapons")]
[SerializeField] GameObject[] guns;
float xThrow, yThrow;
bool isControlEnabled = true;
// Use this for initialization
void Start () {
}
public void OnPlayerDeath(string recievedMessage) {
print("Player is " + recievedMessage);
isControlEnabled = false;
}
// Update is called once per frame
void Update ()
{
if (isControlEnabled)
{
ProcessTranslation();
ProcessRotation();
ProcessFiring();
ProcessAileronRoll();
}
}
private void ProcessAileronRoll()
{
if (Input.GetKeyDown(KeyCode.E)){
print("barrel Roll");
}
}
void ProcessFiring()
{
if (CrossPlatformInputManager.GetButton("Fire"))
{
SetGunsActive(true);
}
else
{
SetGunsActive(false);
}
}
private void SetGunsActive(bool isActive)
{
foreach (GameObject gun in guns)
{
var emissionModule = gun.GetComponent<ParticleSystem>().emission;
emissionModule.enabled = isActive;
}
}
private void ProcessRotation()
{
float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
float pitchDueToControlThrow = yThrow * controlPitchFactor;
float pitch = pitchDueToControlThrow;
float yaw = xThrow * controlYawFactor;
float roll = xThrow * controlRollFactor;
transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
}
private void ProcessTranslation()
{
xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
yThrow = CrossPlatformInputManager.GetAxis("Vertical");
float xOffset = xThrow * xSpeed * Time.deltaTime;
float yOffset = yThrow * xSpeed * Time.deltaTime;
float rawYpos = transform.localPosition.y + yOffset;
float rawXPos = transform.localPosition.x + xOffset;
float YPos = Mathf.Clamp(rawYpos, -yRange, yRange);
float XPos = Mathf.Clamp(rawXPos, -xRange, xRange);
transform.parent = new Vector3(XPos, YPos, transform.localPosition.z);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612183549im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I make my player jump? 1 Answer
WASD and camera movement 1 Answer
How to make an object go the direction it is facing? (Im new) 0 Answers
Face direction of a Vector 3 1 Answer
Make the Player unable to move to opposite direction 1 Answer