Search Unity

Question Input and collision

Discussion in 'Getting Started' started by unity_zRWtH3WFYqN7NA, May 10, 2023.

  1. unity_zRWtH3WFYqN7NA

    unity_zRWtH3WFYqN7NA

    Joined:
    Mar 13, 2022
    Posts:
    2
    Can someone please help me with this? This is a script which does that when the rotating object collides with a obstacle it changes direction and when it collides with another obstacle it changes direction back, I know it could be done better but i try to do it only from my knowledge, without tutorials and realy dont know how to fix it.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class RotatingObject : MonoBehaviour
    {
    public Transform target;
    public float speed;
    public GameObject Obstacle1, Obstacle2;
    bool InTrigger = true;
    bool InTrigger2 = true;
    private Vector3 zAxis = new Vector3(0, 0, 1);

    void Start()
    {

    }
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.E) && InTrigger)
    {
    speed = -3;
    Debug.Log("Pes");
    }
    if (Input.GetKeyDown(KeyCode.E) && InTrigger2)
    {
    speed = 3;
    Debug.Log("Kocka");
    }
    }

    void FixedUpdate()
    {
    transform.RotateAround(target.position, zAxis, speed);

    }
    void OnTriggerStay2D(Collider2D col)
    {
    if (col.gameObject.tag == "Obstacle1")
    {

    Debug.Log("Collision");
    Obstacle1.SetActive(false);
    Obstacle2.SetActive(true);
    InTrigger = true;
    }
    if (col.gameObject.tag == "Obstacle2")
    {

    Debug.Log("Collision");
    Obstacle2.SetActive(false);
    InTrigger2 = true;

    }



    }
    }
     
    Last edited: May 10, 2023