- Home /
Canvas Movement Code Being Disabled
Hi, I am a newcomer to Unity, and have script which was working fine, moving background sprite childed to Canvas object when player presses keys to control movement.
Now it has become disabled somehow, when I press W or D keys at beginning of game, I can see the Pos Y or Pos X attributes in the Inspector change briefly, then reset back to original values making the image shake a little but not move properly. I.e.the code to move background sprite is still running, but then something is bringing it back after. This happened after I have been trying to get a Timeline to play at beginning of this Scene, seems I changed something but don't know what. Even after I have commented out all references to playing Timeline, or Church Animator I was testing, at Start of player script, I still have the problem.
I also tried with Apply Root Motion on and off for the Animators on "Church" background sprite object and "WaterPriest" player object, still same problem.
Previously, I had same issue when playing my first Timeline from script at beginning of scene, but then the problem stopped if I removed timeline from script. Now it's still going on even after I commented out references to timeline or church Animator in the script.
I enclose church inspector image and WaterPriest.cs code, which ran the Timeline, if useful.
Thanks kindly for any tips, or even suggestions best way to rebuild scene from scratch.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
public class WaterPriest : MonoBehaviour
{
//commented but BG still shaking - private PlayableDirector priestDirector;
private AudioSource priestAudioSource;
// array of all the sound clips priest will need in his various actions.
[SerializeField]
private AudioClip[] priestSounds;
private Animator priestAnimator;
private int SwingCount;
private Countdown countdown;
//commented but BG still shaking - private Animator churchAnimator;
void Awake() {
priestAudioSource = this.GetComponent<AudioSource>();
priestAnimator = this.GetComponent<Animator>();
//commented but BG still shaking - priestDirector = GameObject.Find("TimelineEntry").GetComponent<PlayableDirector>();
//commented but BG still shaking - churchAnimator = GameObject.Find("Church").GetComponent<Animator>();
SwingCount = 0;
}
private void Start() {
countdown = GameObject.Find("Countdown").GetComponent<Countdown>();
//countdown.enabled = false;
//churchAnimator.Play("");
//priestDirector.Play();
//countdown.SetActive(true);
}
// unused Invokable method, as now only need to tap cross when T key pressed
private void TapCross() {
priestAnimator.SetBool("IsTapping", true);
}
private void EnableCountdown() {
print("enablecountdown function entered");
countdown.enabled = true;
}
// setting bool Parameter on animation doesn't produce correct timing behaviour!
// Likewise, using Invoke method also didn't seem to work! below snippets don't work for reference:
// priestAnimator.SetBool("IsTapping", true); CancelInvoke("TapCross"); priestAnimator.SetBool("IsTapping", false); etc.....
private void Update() {
if (Input.GetKeyDown(KeyCode.T)) {
priestAnimator.Play("Tap_Cross");
SwingCount++;
if (SwingCount > 3) {
// TODO: Priest dying sequence, plus clues, warnings about water source!
Destroy(gameObject);
}
}
else {
}
}
// this function called by event in the cross swing animation clip.
// check in the Inspector that the array index in code below corresponds to the right sound for this animation!
private void CrossSwing() {
if (priestAudioSource) {
if (priestSounds[0]) {
priestAudioSource.PlayOneShot(priestSounds[0]);
} else {
Debug.LogWarning("Audio clip not found priestSounds[0] for cross swing sound.");
}
}
}
private void PriestRise() {
print("priestrise triggered");
// this works when root is on, and sets to correct rotation: this.transform.rotation = Quaternion.Euler(0,0,90);
// for example, this also works when apply root is on: this.transform.position += new Vector3(0.5f, 0);
// TODO: move church bg down x per frame, until priest is in the sky, then stop him, then change level
}
}
Answer by bunnynsnake · Apr 17, 2018 at 09:34 PM
what function are you calling the movement under? Update or Fixed Update?
$$anonymous$$ovement of church background is called under Update function of its class thanks
Answer by Will_Croxford · Apr 17, 2018 at 10:43 PM
Thanks @bunnynsnake , I just realised, by copying Scene then removing things, it is the Animator component of this Church object (see my picture above) that somehow causes this movement. When I remove this component, the movement script runs again.
But now I still want to animate the church background sprite on entry into the scene.
Does anyone know why adding the animator component causes this problem? Should I be adding Animator only to parent "Canvas" object (again see picture above)? I thought that since I was only animating the church sprite, it wouldn't matter, tx any advice.
I put this as answer as it is more than a comment I guess, but I don't know why this is happening still.
Spoke too soon, I have same problem again now, after moving Animator component to canvas, when I first ran timeline with just background sprite moving, it seemed to play timeline O$$anonymous$$, then I made some adjustments to church animation, choosing edit animation from within Timeline. I think maybe at this point somehow problem re-appeared. Now it seems, placed inside parent Canvas object now, the Animator controller for the church background stops animation from running. When I remove this from reference from Animator component (without needing to remove the component), the background control movement works again. I'm still confused, will try ask this as new question now.
In case anyone else looks at this, it was the Apply Root $$anonymous$$otion in the end that was the problem. But also, it was unnecessary and probably bad idea to have the church image as a child of a canvas object. As the player sprite is moving all over the picture, I changed the picture to a Quad object in unity, not related to canvas at all. Canvas is for UI objects only you want to display info to the player and so on, things that keep same position on screen as you move around, so the background image in this case was silly to child to a canvas. To begin a scene with an animation, turn Apply Root $$anonymous$$otion off in the Inspector for everything with Animator component, then when animation finishes, and want to move the church image with code, need in the code`churchAnimator.applyRoot$$anonymous$$otion = true;` and priestAnimator.applyRoot$$anonymous$$otion = true
.
Could also do this with event just before end of the animation clip, if you do right at the last frame it can be a problem I found, I think this kind of bug perhaps mentioned other places also. Helpfully, I had not put the church script in the question which does make it trickier to see probably!