Unity converting int to double [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 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);

Related

error CS1525: Invalid expression term '-=' [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 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.

Precomputation of constants (Is there a performance difference?) [duplicate]

This question already has answers here:
Does C# Compiler calculate math on constants?
(2 answers)
Closed 1 year ago.
Is the compiler 'smart enough' to precompute this, or will it have a performance impact?
const float num = 0.8660254f; // Sqrt(3) / 2
while(true)
{
float h = num * GetSomeNumber();
}
vs
while(true)
{
float h = (Sqrt(3) / 2 ) * GetSomeNumber();
}
vs
const float num = 1.7320508f; // Sqrt(3)
while(true)
{
float h = (num / 2) * GetSomeNumber();
}
And is there any reason to (not) precompute?
First, the compiler does not do any computation. Computation or execution of the code is a runtime thing. The compiler is smart enough to do multiple adjustments to the code for performance, but it does not have any idea about what method you are calling at runtime. So it won't know how your method Sqrt behaves at runtime. And hence, it will not be able to perform calculations beforehand during compilation.

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;
}

Need help to convert formula into C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I got this formula,
R/Rs = (5800/9500)2(2.5123.37)1/2 = 1.76
How do I turn that into C# so that the value is 1.76. Don't understand what you do with the 2 and 1/2?
Formula is from http://skyserver.sdss.org/dr5/en/proj/advanced/hr/radius1.asp
You are looking for Math.Pow
Math.Pow(5800d/9500d, 2)*Math.Pow(Math.Pow(2.512, 3.37),0.5);
And using 5800d/9500d is important here (forcing double, one of the d's should do), as it would otherwise do integer division, leaving you with 0^2 and overall a big 0...
If you put this into a method taking the necessary double values that should be irrelevant.
You can do :
double res = Math.Pow(5800 / 9500d, 2) * Math.Sqrt(Math.Pow(2.512, 3.37));
Console.WriteLine(res.ToString("0.00"));
output :
1.76
Working demo
A power of 0.5 is a square root.
Its
double i = 5800.0 / 9500;
i = Math.Pow(i, 2);
double x = Math.Pow(2.512, 3.37);
x = Math.Sqrt(x);
x = x * i;
x = Math.Round(x, 2);
OR
Math.Round(Math.Pow(5800.0 / 9500, 2) * Math.Sqrt(Math.Pow(2.512, 3.37)), 2)
The trick is here is in the first line itself. If you will divide 5800 by 9500, it will return zero as division will happen in integers. So to do an actual division resulting in fractions one the the values have to be converted into decimal which i did by converting 5800 to 5800.0

Convert Float to Int [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
So I've got a project I'm working on. This is the only error I have:
Cannot implicitly convert type 'float' to 'int'.
I understand somewhat what that means. I just need help converting my float to int.
This is just an example of one of the floats:
float key = 0.5f;
int key = 53;
Here's the specific code section:
// price in scrap, e.g. 29 / 9 = 3.33 ref
static int BuyPricePerTOD = 21;
// price in scrap, e.g. 31 / 9 = 3.55 ref
static float SellPricePerTOD = BuyPricePerTOD + 0.5F;
static int BuyPricePerKey = 53;
static float SellPricePerKey = BuyPricePerKey + 0.5F;
static int TimerInterval = 170000;
static int InviteTimerInterval = 2000;
int UserWeapAdded,UserScrapAdded,UserRecAdded,UserRefAdded,
UserKeysAdded,UserTODAdded,BotTODsAdded,BotKeysAdded,
BotScrapAdded,BotRecAdded,BotRefAdded,InventoryMetal,
InventoryScrap,InventoryRec,InventoryRef,InventoryKeys,
InventoryTOD,PreviousTODs,PreviousKeys,WhileLoop,InvalidItem = 0;
float UserMetalAdded, BotMetalAdded, OverpayNumKeys,
OverpayNumTOD, ExcessInScrapKey, ExcessInScrapTOD = 0.0F;
double ExcessRefinedKey, ExcessRefinedTOD = 0.0;
Firstly, there are integers and floating-point numbers. Integers are always whole numbers, such as 0, 1, -32, 42 and 1337. On the other hand, floating-point numbers can have a fractional part: 0, 1, -32.1, 42.7 and 123.456788 are all valid floating-point numbers.
When converting between integers (int) and floating-point (float) numbers, you can do this:
int someInt = 42;
float someFloat = someInt; // 42.0f
But you can't do this:
float someFloat = 42.7f;
int someInt = someFloat; // ?
The reason the first conversion is possible, is that converting the integer number (int) to a floating-point number (float) does not change the number. It is a safe conversion, and therefore can be done implicitly.
The reason the second conversion is not allowed, is that converting the floating-point number (which may have a fractional part) to an integer number (that never has a fractional part) must drop the fractional part of the number, i.e. it becomes a different number. This is not safe, and can therefore only be done explicitly.
To explicitly convert one type of number to another, you use a cast. That's the parentheses before the number with the type of the number that you want to convert it to.
float someFloat = 42.7f;
int someInt = (int)someFloat; // 42
Note that the fractional part of the floating-point number was dropped. It's as if it has been rounded towards zero. If you want to round the floating-point number to the nearest whole number, use the Math.Round method.
float someFloat = 42.7f;
int someInt = (int)Math.Round(someFloat); // 43
Try this :
int numInt = (int)Math.Ceiling(numFloat);
msdn documentation
You may want Math.Round() or Math.Floor() by the way.
Example :
float numFloat = 1.5f;
int testCeiling = (int)Math.Ceiling(numFloat);
int testFloor = (int)Math.Floor(numFloat);
int testRound = (int)Math.Round(numFloat);
Console.WriteLine("testCeiling = {0}", testCeiling.ToString());
Console.WriteLine("testFloor = {0}", testFloor.ToString());
Console.WriteLine("testRound= {0}", testRound.ToString());
output :
testCeiling = 2
testFloor = 1
testRound= 2

Categories