5
\$\begingroup\$

https://www.youtube.com/watch?v=m-J5sCRipA0 - this tutorial by brackeys shows how rotate your 2d gun using your mouse. How can I edit this script to add an auto rotating system. Where the gun keeps rotating and when you press a certain button(i.e. space) it stops and shoots.

This is the code for rotating your 2d gun using your mouse;

void Update(){
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; // subtracting the position of the player from the mouse mous position  
difference.Normalize(); // normalizing the vector. meaning that all the sum of the vector will be equal to 1
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; //find the angle in degrees
transform.rotation = Quaternion.Euler(0f, 0f, rotZ );

 }

Edit: I solved my previous question, but I want it to go anti clockwise after it reaches 0 degrees or when the gun faces right and clockwise again when it reaches 180 degrees or faces left.

this is my updated code -:

   void Update(){

   transform.Rotate(0, 0, -3.0f);

   }
\$\endgroup\$
  • 1
    \$\begingroup\$ Share what have you tried? \$\endgroup\$ – Sourav Paul Jul 5 '16 at 3:14
  • \$\begingroup\$ If you know the exact rotation values and they won't change, just make an animation and in code just shoot. (This is ofc if you don't need it to stop for some time after shooting). \$\endgroup\$ – Candid Moon _Max_ Mar 6 '17 at 9:24
  • \$\begingroup\$ In the code, you can easily achieve it by using coroutines inside coroutines. \$\endgroup\$ – Candid Moon _Max_ Mar 6 '17 at 9:25
0
\$\begingroup\$

You could do something like this:

private int direction = 1;

void Update(){
  if(transform.rotation.z == 0 || transform.rotation.z == 180){
    direction = -direction;
  }
  transform.Rotate(0,0,-3f*direction);
}

I didn't really understand what you meant when you said how you wanted the direction of rotation to switch, so you could just replace the clauses in the if-statement to fit whatever you need.

| improve this answer | |
\$\endgroup\$
  • 5
    \$\begingroup\$ Checking for exact equality on a floating point value is very dicey. It's easy to overshoot the threshold by some tiny decimal, causing your check to fail. Your update code also doesn't include Time.deltaTime for framerate independence, so the speed of rotation will fluctuate with the framerate. \$\endgroup\$ – DMGregory Oct 14 '16 at 20:49
0
\$\begingroup\$

The following code is untested.


public class Turret {

[SerializeField] private float _minEulerRotation;
[SerializeField] private float _maxEulerRotation;

[SerializeField] private float _rotationAmount;

private CoolDown _coolDown;

void Start(){

    this._rotationAmount *= Time.fixedDeltaTime;

    this.StartCoroutine(this.RotationProcess());

}

private IEnumerator RotationProcess()
{
    while (true)
    {
        yield return this.StartCoroutine(this.RotateToMin());
        yield return this.StartCoroutine(this.RotateToMax());
    }
}

private IEnumerator RotateToMin()
{   
    while(transform.rotation.eulerAngles."Axis you need" > this._minEulerRotation){

        transform.Rotate("Your vector3" * this._rotationAmount);

        if(this._coolDown.IsActive)
        {
            yield return this._coolDown.Delay;
        }

        yield return null;
    }

    transform.rotation = Quaternion.Euler("Your vector3" * this._minEulerRotation);
}

private IEnumerator RotateToMax()
{
    while(transform.rotation.eulerAngles."Axis you need" < this._maxEulerRotation){

        transform.Rotate("Your vector3" * this._rotationAmount);

        if(this._coolDown.IsActive)
        {
            yield return this._coolDown.Delay;
        }

        yield return null;
    }

    transform.rotation = Quaternion.Euler("Your vector3" * this._maxEulerRotation);
}

public void Shoot()
{
    ...... some cooldown stuff
    this._coolDown.StartCountdown();
}
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.