Çözüldü Unity karakter sağ ve sola yer değiştiriyor

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

EgoWaixx

Hectopat
Katılım
11 Haziran 2021
Mesajlar
574
Çözümler
4
Daha fazla  
Cinsiyet
Erkek
Unity'de kod yazdım fakat düzgün çalışmıyor. Kod aslında çalışıyor ama karakter yer değişiyor. Benim istediğin sadece sağa ve sola dönmesi, yani A tuşuna basınca sola D'ye basınca sağa dönmesi lazım. Bunları yapıyor ama yer değiştiriyor.





 
Çözüm
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    Rigidbody2D rgb;
    Vector3 velocity;
    public SpriteRenderer spriteRenderer;

    float speedAmount = 7f;
    float jumpAmount = 6f;
    // Start is called before the first frame update
    void Start()
    {
        rgb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        velocity = new Vector3(Input.GetAxis("Horizontal"), 0f);
        transform.position += velocity * speedAmount * Time.deltaTime;

        if (Input.GetButtonDown("Jump") && Mathf.Approximately(rgb.velocity.y, 0))
        {
            rgb.AddForce(Vector3.up * jumpAmount, ForceMode2D.Impulse);
        }

        if (Input.getAxisRaw("Horizontal") == -1) {
            spriteRenderer.flipX = true;
        } else if (Input.getAxisRaw("Horizontal") == 1) {
            spriteRenderer.flipX = false;
        }
    }
}
Inspectordan Sprite Renderer objesini scripte verin.
Sprite ters çevirebilirsiniz.
C#:
public SpriteRenderer spriteRenderer;
C#:
if (Input.getAxisRaw("Horizontal") == -1) {
    spriteRenderer.flipX = true;
} else if (Input.getAxisRaw("Horizontal") == 1) {
    spriteRenderer.flipX = false;
}
 

Tam halini atar mısın kanka.
 
Kodları resim olarak değilde yazı halinde atarsanız ekleyebilirim.
 
Kodları resim olarak değil de yazı halinde atarsanız ekleyebilirim.

Tamam atıyorum.
Kod:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    Rigidbody2D rgb;
    Vector3 velocity;

    float speedAmount = 7f;
    float jumpAmount = 6f;
    // Start is called before the first frame update
    void Start()
    {
        rgb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        velocity = new Vector3(Input.GetAxis("Horizontal"), 0f);
        transform.position += velocity * speedAmount * Time.deltaTime;

        if (Input.GetButtonDown("Jump") && Mathf.Approximately(rgb.velocity.y, 0))
        {
            rgb.AddForce(Vector3.up * jumpAmount, ForceMode2D.Impulse);
        }

        if (Input.GetAxisRaw("Horizontal") == -1)
        {
            transform.rotation = Quaternion.Euler(0f, 180f, 0f);
        }
        else if (Input.GetAxisRaw("Horizontal") == 1)
        {
            transform.rotation = Quaternion.Euler(0f, 0f, 0f);
        }
    }
}
 
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    Rigidbody2D rgb;
    Vector3 velocity;
    public SpriteRenderer spriteRenderer;

    float speedAmount = 7f;
    float jumpAmount = 6f;
    // Start is called before the first frame update
    void Start()
    {
        rgb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        velocity = new Vector3(Input.GetAxis("Horizontal"), 0f);
        transform.position += velocity * speedAmount * Time.deltaTime;

        if (Input.GetButtonDown("Jump") && Mathf.Approximately(rgb.velocity.y, 0))
        {
            rgb.AddForce(Vector3.up * jumpAmount, ForceMode2D.Impulse);
        }

        if (Input.getAxisRaw("Horizontal") == -1) {
            spriteRenderer.flipX = true;
        } else if (Input.getAxisRaw("Horizontal") == 1) {
            spriteRenderer.flipX = false;
        }
    }
}
Inspectordan Sprite Renderer objesini scripte verin.
 
Çözüm

Nasıl yani tam olarak anlatır mısınız kodu aynı yaptım sola dönmemeye başladı.


Kod:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour.
{
 Rigidbody2D rgb;
 Vector3 velocity;
 public SpriteRenderer spriteRenderer;

 float speedAmount = 7f;
 float jumpAmount = 6f;
 // Start is called before the first frame update.
 void Start()
 {
 rgb = GetComponent<Rigidbody2D>();
 }

 // Update is called once per frame.
 void Update()
 {
 velocity = new Vector3(Input.GetAxis("Horizontal"), 0f);
 transform.position += velocity * speedAmount * Time.deltaTime;

 if (Input.GetButtonDown("Jump") && Mathf.Approximately(rgb.velocity.y, 0))
 {
 rgb.AddForce(Vector3.up * jumpAmount, ForceMode2D.Impulse);
 }

 if (Input.GetAxisRaw("Horizontal") == -1)
 {
 spriteRenderer.flipX = true;
 }
 else if (Input.GetAxisRaw("Horizontal") == 1)
 {
 spriteRenderer.flipX = false;
 }
 }
}

Eki Görüntüle 1598844




Hangisini vereceğim. WhatsApp yazan karakter.
 
Son düzenleme:
Rigidbody 2D yazan yerin üstünde Sprite Renderer olması gerek. Onu sürükleyip bırakın.
 

Bu konuyu görüntüleyen kullanıcılar

Bu siteyi kullanmak için çerezler gereklidir. Siteyi kullanmaya devam etmek için çerezleri kabul etmelisiniz. Daha Fazlasını Öğren.…