A
A
Alex Blinov2019-07-14 11:39:33
C++ / C#
Alex Blinov, 2019-07-14 11:39:33

Doesn't switch True False. What to do?

spoiler
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
 
public class RotateTank2 : MonoBehaviour 
{ 
 
 
 public Transform Selftransform;  
 
 public int speed = 6; 
 
 public bool rotate = true;
 
 
 
 
 // Use this for initialization 
 void Start() 
 { 

 
 } 
 void RightRotate() 
 { 
 transform.Rotate(0, 0, -3); 
 } 
 void LeftRotate() 
 { 
 transform.Rotate(0, 0, 3); 
 } 
 // Update is called once per frame 
 void FixedUpdate() 
 { 
 
 
 if (rotate == true && !Input.GetKey(KeyCode.D)) 
 { 
 RightRotate(); 
 
 } 
 
 if (rotate == false && !Input.GetKey(KeyCode.D)) 
 { 
 LeftRotate(); 
 
 } 
 
 if (Input.GetKey(KeyCode.D))
 
 
 rotate = !rotate; 
 
 Selftransform.position += Selftransform.up * speed * Time.deltaTime; 
 
 } 
 else 
 { 
 
 } 
 
 } 
}

I made a unit switch that, when pressed, changes the direction of rotation, but it does not work. Sometimes it switches, and sometimes it does not. Tell me what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2019-07-14
@CEPII

Input.GetKey(KeyCode.D)
here is the main problem. except for the hodgepodge of flags that confuse you))
read how
GetKey
GetKeyDown
GetKeyUp differs.
and understand. that GetKey - can work several times per frame.
and another important point is to do INPUT on Update (each card for responsiveness and adequacy).
not on FixedUpdate (this is for physics)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question