Çö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.

Meteakyz

Decapat
Katılım
11 Haziran 2021
Mesajlar
383
Çözümler
3
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.

1670672206805.png


1670672293458.png


1670672301375.png
 
Çö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;
}
 
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ğ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
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.

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

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.

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

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.

1670683396957.png


Hangisini vereceğim. WhatsApp yazan karakter.
 
Son düzenleme:

Geri
Yukarı