I’ve been struggling in vain to implement some code so my character’s attack animation will only work once the player has entered the next level. I’ve tried using two scripts and referencing each one via static variables, but so far I’ve been unsuccessful. Basically, in the starting area, I don’t want the attack button (space) to work. Eventually the player will get their weapon and enter the next level, whereby they can attack with space. These are the two scripts I’ve been using:
PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Animator anim;
public float moveSpeed;
public float jumpForce;
public bool jumped;
public bool attack;
public float gravityScale;
public float knockBackForce;
public float knockBackTime;
public float invincibilityLength;
public Renderer playerRenderer;
public Material textureChange;
public Material textureDefault;
private float jumpDelay;
private Vector3 moveDirection;
private float knockBackCounter;
private float invincibilityCounter;
private CharacterController controller;
void Start()
{
Cursor.visible = false;
controller = GetComponent<CharacterController>();
/*jumped = false;
if(jumpDelay <= 0)
{
jumpDelay = 5;
}*/
}
void Update()
{
if (knockBackCounter <= 0)
{
float moveHorizontal = Input.GetAxis("Horizontal");
moveDirection = new Vector3(moveHorizontal * moveSpeed, moveDirection.y);
if (moveHorizontal > 0)
{
transform.eulerAngles = new Vector3(0, 90);
}
else if (moveHorizontal < 0)
{
transform.eulerAngles = new Vector3(0, -90);
}
if (controller.isGrounded)
{
moveDirection.y = -1f;
if (Input.GetKey(KeyCode.KeypadPlus))
{
moveDirection.y = jumpForce;
jumped = true;
//StartCoroutine(SpamBlockco());
}
else if (!Input.GetKey(KeyCode.KeypadPlus))
{
jumped = false;
}
if (Input.GetKey(KeyCode.Space))
{
attack = true;
playerRenderer.material = textureChange;
}
else if (!Input.GetKey(KeyCode.Space))
{
attack = false;
playerRenderer.material = textureDefault;
}
}
}
else
{
knockBackCounter -= Time.deltaTime;
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
a
anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
if (attack)
{
anim.SetTrigger("Attack");
}
}
/*public void Level1()
{
if (LevelManager.levelLoaded)
{
if (attack)
{
anim.SetTrigger("Attack");
}
if (controller.isGrounded)
{
if (Input.GetKey(KeyCode.Space))
{
attack = true;
playerRenderer.material = textureChange;
}
else if (!Input.GetKey(KeyCode.Space))
{
attack = false;
playerRenderer.material = textureDefault;
}
}
}
}*/
public void Knockback(Vector3 direction)
{
knockBackCounter = knockBackTime;
moveDirection = direction * knockBackForce;
moveDirection.y = knockBackForce;
}
/*public Vector3 GetTravelDirection()
{
return moveDirection.normalized;
}*/
/*public IEnumerator SpamBlockco()
{
if (moveDirection.y == jumpForce)
{
yield return new WaitForSeconds(jumpDelay);
}
yield return null;
jumped = false;
}*/
}
Level Manager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelManager : MonoBehaviour
{
public static bool levelLoaded;
void Start()
{
}
public void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Player"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
levelLoaded = true;
Debug.Log("Level loaded");
//Level1();
Debug.Log("Level 1 method called");
}
}
}
Any ideas on how I can do this? Thanks.