Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

input not working (pls help)

Discussion in 'Scripting' started by jupiter-42, Mar 9, 2022.

  1. jupiter-42

    jupiter-42

    Joined:
    Feb 9, 2022
    Posts:
    3
    Can you please help me fix this script?
    So it is supposed to rotate when a or d is pressed, but it doesn't seem to work.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class roll_cs : MonoBehaviour
    {
    public GameObject player;
    public float moveSpeed = 500f;
    Rigidbody2D rb;
    Vector2 v;
    public void reset()
    {
    transform.position = new Vector3(0f, 0f, -0.1f);
    }

    void Start()
    {
    rb = GetComponent<Rigidbody2D>();
    Debug.Log("oe");
    }

    void Update()
    {

    if (Input.GetKeyDown(KeyCode.D))
    {
    rb.angularVelocity = -moveSpeed;
    Debug.Log("oe");
    }
    else if (Input.GetKeyDown(KeyCode.A))
    {
    rb.angularVelocity = moveSpeed;
    Debug.Log("oe");
    }

    }
    void OnCollisionEnter2D(Collision2D col)
    {
    if (col.gameObject.tag == "Respawn")
    {
    reset();
    }
    if (col.gameObject.tag == "speedup")
    {
    moveSpeed = 2000f;
    }
    else
    {
    moveSpeed = 1500f;
    }
    if (col.gameObject.tag == "bouncepad")
    {
    v = new Vector2(rb.velocity.x, 10f);
    rb.velocity = v;
    moveSpeed = 2000f;
    }
    if (col.gameObject.tag == "bpad2")
    {
    v = new Vector2(2f, 14f);
    rb.velocity = v;
    moveSpeed = 2500f;
    }
    }
    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    Scripting questions should be posted on the Scripting forum. This question isn't about 2D, it's about input and scripting.

    Also, use code-tags when posting code and please edit your post above.

    I'll move your post.
     
  3. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    @jupiter-42 Your Debug.Log statements are all the same, make them unique so you can track the code execution. Once you do this, please provide your Console output showing your Debug.Log statements. And please use Code Tags, look at a few other posts on here to see how to format your code for the forum.
     
  4. jupiter-42

    jupiter-42

    Joined:
    Feb 9, 2022
    Posts:
    3
    i got this fixed thx!
     
    JeffDUnity3D likes this.