Mathf.Abs always returns 0
Hi everyone. I have a problem with my script in C#. Here the code:
void FixedUpdate () {
controls ();
RaycastHit2D attr = Physics2D.Raycast (transform.position, sphere.position);
if (attr.collider != null) {
float dist_x = Mathf.Abs (attr.point.x - transform.position.x);
float dist_y = Mathf.Abs (attr.point.y - transform.position.y);
if (dist_x < 0.5f && Input.GetKey(KeyCode.Space)) {
attract (attr);
sphere.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (boost_force, 0));
}
}
}
The problem is that dist_x and dist_y are always set to 0. It's not reasonable, because the function Abs wants two float values and in this case I used two float values.
Looks like the raycast is wrong. They're tricky -- read the Docs and the explanations here in UA (most are 3D recasts, but the idea is the same.)
Ok, but if I print attr.point.x in the console, the values are perfectly shown!
Your answer
Follow this Question
Related Questions
Editing animation curves in a script 2 Answers
Having trouble deleting objects from a list after they reach a certain scale. 0 Answers
How To refer to other Object's Components ? 0 Answers
How attach C# script by argument? 3 Answers
What is the most effective way to structure Card Effects in a Single Player game? 1 Answer