Hey Guys, I recently started to learn Unity and I´m with the first problem that I can´t fix by myself. I guess is something basic, but I don´t know the answer. I swear I tried to find the solution by my self, but I can´t find it.
So, the idea is the following. My character moves using the normal input (this works perfectly), but when the player collision with a gameobject (a barrel) should activate a trigger and change to an automate script/animation. But doesn´t work, because there is like a invisible wall and after some seconds, the trigger activate and the script/animation works… But, obviously, I want to move normal until I reach the barrel, and then activate the trigger.
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using Vector3 = System.Numerics.Vector3;
public class Walk : MonoBehaviour
{
private Rigidbody2D Rigidbody2D;
private float Horizontal;
private Animator Animator;
// Start is called before the first frame update
void Start()
{
Rigidbody2D = GetComponent<Rigidbody2D>();
Animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Horizontal = Input.GetAxisRaw("Horizontal");
if (Horizontal < 0.0f) transform.localScale = new UnityEngine.Vector3(-1.0f, 1.0f, 1.0f);
else if (Horizontal > 0.0f) transform.localScale = new UnityEngine.Vector3(1.0f, 1.0f, 1.0f);
Animator.SetBool("walk", Horizontal != 0.0f);
}
private void FixedUpdate()
{
Rigidbody2D.velocity = new UnityEngine.Vector2(Horizontal, Rigidbody2D.velocity.y);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "barrel");
Debug.Log("Trigger Activado");
Animator.SetTrigger("test");
}