I seem to be having issues getting my interact animation to work when my character picks up an object. The object just disappears without the animation playing. However, I did manage to get it working at one point, but then it wouldn’t go back to the idle animation.
I’ve tried experimenting with different bools and trigger conditions in the Animator tab, but it’s just not working. I’ve set up things like isGrounded, buttonPressed, and Interact.
Here’s my PlayerController script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public Animator anim;
public float moveSpeed;
public float jumpForce;
public bool jumped;
public bool attack;
public static bool interact = false;
public float gravityScale;
public float knockBackForce;
public float knockBackTime;
public float invincibilityLength;
public Renderer playerRenderer;
public Material textureChange;
public Material textureDefault;
public bool allowCombat = false;
public bool allowJump;
public bool allowInteract = false;
public string startPoint;
private Vector3 moveDirection;
private Vector3 extraDirections;
private float knockBackCounter;
private float invincibilityCounter;
private CharacterController controller;
private static bool playerExists;
void Start()
{
Cursor.visible = false;
controller = GetComponent<CharacterController>();
//Every bool starts on false
if (!playerExists)
{
playerExists = true;
DontDestroyOnLoad(transform.gameObject);
}
else
{
Destroy(gameObject);
}
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("level 1"))
{
allowCombat = true;
allowJump = true;
}
else if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area"))
{
allowCombat = false;
allowJump = true;
}
else if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("hut_interior"))
{
allowCombat = false;
allowJump = false;
}
}
void Update()
{
if (knockBackCounter <= 0)
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
moveDirection = new Vector3(moveHorizontal * moveSpeed, moveDirection.y);
extraDirections = new Vector3(moveVertical * moveSpeed, extraDirections.y);
if (moveHorizontal > 0)
{
transform.eulerAngles = new Vector3(0, 90);
}
else if (moveHorizontal < 0)
{
transform.eulerAngles = new Vector3(0, -90);
}
//To possibly prevent diagonal movement with some control setups, try adding 'else if'
else if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("hut_interior"))
{
if (moveVertical > 0)
{
transform.eulerAngles = new Vector3(0, 0);
}
else if (moveVertical < 0)
{
transform.eulerAngles = new Vector3(0, 180);
}
}
if (controller.isGrounded)
{
if (allowJump)
{
moveDirection.y = -1f;
//GetKeyDown will require the player to press the button each time they want to jump. GetKey will allow the player to spam the jump button if they keep pressing it down.
if (Input.GetKeyDown(KeyCode.KeypadPlus) || Input.GetKeyDown("joystick button 1"))
{
moveDirection.y = jumpForce;
jumped = true;
}
else if (!Input.GetKeyDown(KeyCode.KeypadPlus) || !Input.GetKeyDown("joystick button 1"))
{
jumped = false;
}
}
else
{
allowJump = false;
}
if (allowCombat)
{
if (Input.GetKey(KeyCode.Space) || Input.GetKey("joystick button 7"))
{
attack = true;
playerRenderer.material = textureChange;
}
else if (!Input.GetKey(KeyCode.Space) || !Input.GetKey("joystick button 7"))
{
attack = false;
playerRenderer.material = textureDefault;
}
}
else if (!allowCombat)
{
attack = false;
playerRenderer.material = textureDefault;
}
if (allowInteract)
{
if (Input.GetKeyDown(KeyCode.Return))
{
interact = true;
}
}
else if (!allowInteract)
{
interact = false;
}
}
}
else
{
knockBackCounter -= Time.deltaTime;
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
anim.SetBool("isGrounded", controller.isGrounded);
anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
if (attack)
{
anim.SetTrigger("Attack");
}
if (interact)
{
anim.SetBool("buttonPressed", controller.isGrounded);
anim.SetBool("Interact", controller.isGrounded);
}
}
public void Knockback(Vector3 direction)
{
knockBackCounter = knockBackTime;
moveDirection = direction * knockBackForce;
moveDirection.y = knockBackForce;
}
}
Pickup:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickup : MonoBehaviour
{
public GameObject[] buttonPrompts;
public GameObject rayGun;
public GameObject pickupLight;
public bool allowInteract = false;
private int xbox360Controller = 0;
private int ps4Controller = 0;
void Start()
{
}
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
allowInteract = true;
PlayerController.interact = true;
ControllerDetection();
if (ps4Controller == 1)
{
PS4Prompts();
}
else if (xbox360Controller == 1)
{
Xbox360Prompts();
}
else
{
PCPrompts();
}
}
}
void Update()
{
if (allowInteract)
{
if (xbox360Controller == 1)
{
if (Input.GetKeyDown("joystick button 2"))
{
rayGun.SetActive(false);
pickupLight.SetActive(false);
}
}
else if (ps4Controller == 1)
{
if (Input.GetKeyDown("joystick button 0"))
{
rayGun.SetActive(false);
pickupLight.SetActive(false);
}
}
else if (Input.GetKeyDown(KeyCode.Return))
{
rayGun.SetActive(false);
pickupLight.SetActive(false);
}
}
}
public void Hide()
{
buttonPrompts[0].SetActive(false);
buttonPrompts[1].SetActive(false);
buttonPrompts[2].SetActive(false);
}
public void Xbox360Prompts()
{
buttonPrompts[1].SetActive(true);
Invoke("Hide", 3f);
}
public void PS4Prompts()
{
buttonPrompts[2].SetActive(true);
Invoke("Hide", 3f);
}
public void PCPrompts()
{
buttonPrompts[0].SetActive(true);
Invoke("Hide", 3f);
}
public void ControllerDetection()
{
string[] names = Input.GetJoystickNames();
for (int x = 0; x < names.Length; x++)
{
//print(names[x].Length);
if (names[x].Length == 19)
{
//print("PS4 CONTROLLER IS CONNECTED");
ps4Controller = 1;
xbox360Controller = 0;
if (ps4Controller == 1)
{
Debug.Log("PS4 controller detected");
}
}
else if (names[x].Length == 33)
{
//print("XBOX 360 CONTROLLER IS CONNECTED");
ps4Controller = 0;
xbox360Controller = 1;
if (xbox360Controller == 1)
{
Debug.Log("Xbox 360 controller detected");
}
}
else
{
ps4Controller = 0;
xbox360Controller = 0;
}
if (xbox360Controller == 0 && ps4Controller == 0)
{
Debug.Log("No controllers detected");
}
}
}
}