Hello everybody,
I have successfully done the first part. The character takes the key and goes through the locked door to the second section. But while designing the second part, my character slips while attacking. How to prevent the character from sliding while attacking and moving in Unity? Do I need a setting related to the basic layers or do I need a change in the scripts? Anyone know the solution?
Thanks… ![]()
Here are the codes…
Here is my Score script:
using TMPro;
using UnityEngine;
public class Score : MonoBehaviour
{
[SerializeField]
public int score;
public TextMeshProUGUI scoreUI;
// Update is called once per frame
void Update()
{
scoreUI.text = score.ToString();
}
void OnCollisionEnter2D(Collision2D other)
{
UnityEngine.Debug.Log("Collider is Working");
if (other.gameObject.tag == "altin")
{
score += 10;
}
else if (other.gameObject.tag == "gumus")
{
score += 5;
}
else if(other.gameObject.tag == "bronz")
{
score += 2;
}
}
}
Here is my Kapi(Door) Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Kapi: MonoBehaviour
{
[SerializeField]
private GameObject anahtarVar;
[SerializeField]
private GameObject kapiAcik;
[SerializeField]
private AudioSource kapiKilitliSes;
[SerializeField]
private AudioSource kapiAcikSes;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player" && anahtarVar.activeSelf)
{
kapiAcik.SetActive(true);
other.gameObject.SetActive(false);
kapiAcikSes.Play();
SceneManager.LoadScene(1);
}
else if(other.gameObject.tag == "Player")
{
kapiKilitliSes.Play();
}
}
}
And here is my Player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
private Rigidbody2D myRigidbody;
private Animator myAnimator;
[SerializeField]
private GameObject anahtarVar;
[SerializeField]
private float hiz;
private bool sagaBak;
[SerializeField]
private AudioSource coinSes;
[SerializeField]
private AudioSource anahtarSes;
private bool atak;
// Start is called before the first frame update
void Start()
{
sagaBak = true;
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Kontroller();
}
void FixedUpdate()
{
float yatay = Input.GetAxis("Horizontal");
TemelHareketler(yatay);
YonCevir(yatay);
AtakHareketleri();
DegerleriResetle();
}
private void TemelHareketler(float yatay)
{
if (!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("atak"))
{
myRigidbody.velocity = new Vector2(yatay * hiz, myRigidbody.velocity.y);
}
myAnimator.SetFloat("karakterHizi", Mathf.Abs(yatay));
}
private void AtakHareketleri()
{
if (atak && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("atak"))
{
myAnimator.SetTrigger("atak");
myRigidbody.velocity = Vector2.zero;
}
}
private void Kontroller()
{
if (Input.GetKeyDown(KeyCode.T))
{
atak = true;
}
}
private void YonCevir(float yatay)
{
if (yatay > 0 && !sagaBak || yatay < 0 && sagaBak)
{
sagaBak = !sagaBak;
Vector3 yon = transform.localScale;
yon.x *= -1;
transform.localScale = yon;
}
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == ("altin"))
{
other.gameObject.SetActive(false);
coinSes.Play();
}
else if (other.gameObject.tag == ("gumus"))
{
other.gameObject.SetActive(false);
coinSes.Play();
}
else if (other.gameObject.tag == ("bronz"))
{
other.gameObject.SetActive(false);
coinSes.Play();
}
if (other.gameObject.tag == ("anahtar"))
{
other.gameObject.SetActive(false);
anahtarVar.SetActive(true);
anahtarSes.Play();
}
}
private void DegerleriResetle()
{
atak = false;
}
}