error CS1525: Invalid expression term '-=' [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
Im trying to add a simple attack function to my game, I use Time.DeltaTime to add a cooldown, here is the code:
float attackCD = 0.2;
if (attackCD < 0)
{
if (animation == 1)
{
Player.Play("SwordSwing", 0, 0.0f);
int animation = 2;
}
if (animation == 2)
{
Player.Play("SwordSwing2", 0, 0.0f);
int animation = 1;
}
}
float attackCD -= time.DeltaTime;
This error for some reason results in error CS1525: Invalid expression term '-='
I checked the documentations on multiple websites and "-=" seems to be an existing thing, what am I doing wrong?

You've already declared attackCD here:
float attackCD = 0.2;
but this code tries to declare it again:
float attackCD -= time.DeltaTime;
By specifying the type at the beginning you are indicating that you are declaring a new variable of that type. You already know how to use an existing variable because you're doing it elsewhere. Do the same here:
attackCD -= time.DeltaTime;
Without the attempted declaration, you can successfully decrement the variable.

Related

Unity converting int to double [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I am getting the players current x velocity, then attempting to multiply it when the player jumps. This information comes in the form of a double, so I've (attempted) to convert it to INT form inside the "rv" variable. However, everytime that I do this, it seems to take the form of a double again.
Error message:
Assets/player.cs(137,39): error CS1503: Argument 1: cannot convert from 'double' to 'float'
Code:
void Jump(){
if (Input.GetKeyDown(KeyCode.Space) && (isGrounded || Time.time - lastTimeGrounded <= rememberGroundedFor || additionalJumps > 0) && sinceDash > dashTime){
if(timesincejump>30){
timesincejump = 0;
int rv = Mathf.RoundToInt(rb.velocity.x);
rb.velocity = new Vector2(rv*1.12, jumpForce);
additionalJumps--;
}
}else{
if(Input.GetKeyDown(KeyCode.Space)){
preJumpedTime = 0;
}
}
}
Unity Vectors requires float values in order to be used;
You need to add the keyword "f" after every decimal number in order to be recognized as a float and not a double. C# expect a suffix to not interpret every decimal number as a double.
var x = 1.2; // This is a double!
var y = 1.2f; // This is float!
So rb.velocity.x comes from rb.velocity wich is a Vector2, and it's values are floats.
Besides that also instead of converting to int and then multiplying with a decimal again, it would be better if you stick to float right away.
There's no need for this
int rv = Mathf.RoundToInt(rb.velocity.x);
Rather store rb.velocity.x only if you will need further, you can directly use this value, like:
rb.velocity = new Vector2(rb.velocity.x * 1.12f, jumpForce);

How to refactor this waterfall of ifs to be more clean and efficient? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I'm working on my flaw when i write code: i never write clean programs in any language.
So, any suggestion to re-write this piece of code in an "elegant" way? Because I don't like this waterfall of ifs...
public float RoundAngle(float angle)
{
if(angle < 45 && angle >= -45){
return 0
}
if(angle < 135 && angle >= 45){
return 90
}
if(angle < 315 && angle >= 225)
{
return 270
}
}
You can use a switch expression with some C# 9 patterns:
public float RoundAngle(float angle)
{
return angle switch
{
<45 and >=-45 => 0,
<135 and >=45 => 90,
<315 and >=225 => 270,
_ => SomeDefaultValue,
};
}
Well, you don't need ifs at all to begin with:
var normalizedAngle = 90 * (int)((angle + 405) / 90) % 360;
That said, ifs are probably easier to understand
Unless you have a really complicated nested if structure which is hard to read and follow, not your case at all, there is nothing actually wrong with your code. IMHO I think its easier to understand than the one liner option.

Correlate two floats using c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I know im an idiot and I am sure this is simple math. But I cannot seem to wrap my head around it here is my situation
When X = 3, I need Y = 0, and when X = 0 I need y = 1;.
I am trying to fill a progress bar based upon how low X is.
The value to fill the progress bar (Y) must be between 0 and 1.
Math?
// "Single" is just like "float"
Single y = (3.0f - x) / 3.0f;
So that
x=3 -> y=0.00
x=2 -> y=0.33
x=1 -> y=0.66
x=0 -> y=1.00
Alternatively:
// different points of view are better
Single y = -(x - 3.0f) / 3.0f;
As I know best from my high school:
y = a*x + b
You must solve equations:
0 = a*3 + b and
1 = a*0 + b
a = -b/3; b =1
So your equation is: y=-1/3*x+1
private float GetProgressValue(float x)
{
return x/-3f + 1f;
}

How can I fix Array index out of range Unity [duplicate]

This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 3 years ago.
Array index is out of range in Unity, need some help!
void OnCollisionEnter2D(Collision2D col) {
rigidbody2D.velocity = new Vector2(0, 0);
transform.rotation = Quaternion.Euler (0, 0, headDownAngle);
if(!isDied) {
audios[1].Play();
animator.SetTrigger("dead");
iTween.ShakePosition(Camera.main.gameObject, new Vector3(0.3f, 0.3f, 0), 0.5f);
}
isDied = true;
isPlaying = false;
I don't see any other arrays being used (Unless one of your other functions use arrays) so it seems this line is causing the error:
audios[1].Play();
Remember Arrays are zero based so the first position in an array is actually 0, so if you are trying to get the first element do:
audios[0].Play().
If you are trying to get the second element, make sure audios has 2 elements.
Array index is out of range is an error caused when you try to use a part of an array that doesn't exist
example
float numOfTests = 2;
AudioSource[numOfTests] tests;
void Start()
{
tests[3].play();
}
since there are only 2 audio sources in the array we can access a third one because it doesn't exist.
I hope this is helpful. I used to code in unity put I moved over to c++ and writing custom engines so if my answer doesn't help that might because I haven't used unity in a year.

C# Method Calculate [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to calculate the perimeter of a circle using method
for some reason I get an error at:
//double p = 2 * Math.PI * r;
I am new to using method please help and show me what I did wrong.
static void Main(string[] args)
{
double perimeter;
Console.Write("Enter Perimeter: ");
double.TryParse(Console.ReadLine(), out perimeter);
double per = PerimeterOfCircle(perimeter);
Console.WriteLine("\nPerimeter of Circle = {0}",
per.ToString("F3"));
Console.ReadKey();
}
static double PerimeterOfCircle(double p)
{
double p = 2 * Math.PI * r;
return p;
}
Looks like you have the parameter named incorrectly. Change it to r:
static double PerimeterOfCircle(double r) // <-- changed from p to r here
{
double p = 2 * Math.PI * r;
return p;
}
Also you can embed the format string within WriteLine:
Console.WriteLine("\nPerimeter of Circle = {0:F3}", per);
Simple: You did not provide a value for r. The code does not even compile.

Categories