- Home /
Need health to decrease with movement in Javascript
No errors, but what I've got doesn't work and I cannot figure out why. All I'm looking for is the variable called health to go down by 1 point with every movement in the inspector. The script is attached to a player prefab. N.B. Even if I change the value of the variable in the script it remains the same in the inspector. Here's the code. Some help would be much appreciated.
pragma strict
var hit : RaycastHit;
var utran;
var newTile : GameObject;
public var health : int;
function Start () {
health = 300;
}
function Update () {
utran = new Vector3 (transform.position.x, transform.position.y, transform.position.z-0.5);
if (Input.GetKeyUp(KeyCode.A))
{
if (Physics.Raycast (transform.position, -Vector3.right, hit, 25))
{
if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
{
transform.position.x -= 2.75;
health -= 1;
}
}
}
if (Input.GetKeyUp(KeyCode.D))
{
if (Physics.Raycast (transform.position, Vector3.right, hit, 25))
{
if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
{
transform.position.x += 2.75;
health -= 1;
}
}
}
if (Input.GetKeyUp(KeyCode.W))
{
if (Physics.Raycast (transform.position, Vector3.up, hit, 25))
{
if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
{
transform.position.y += 2.75;
health -= 1;
}
}
}
if (Input.GetKeyUp(KeyCode.S))
{
if (Physics.Raycast (transform.position, -Vector3.up, hit, 25))
{
if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
{
transform.position.y -= 2.75;
health -= 1;
}
}
}
if (Physics.Raycast (utran, Vector3.forward, hit, 5))
{
if (hit.transform.tag == "Exit")
{
Application.LoadLevel(Application.loadedLevel);
}
}
if (Physics.Raycast (utran, Vector3.forward, hit, 5))
{
if (hit.transform.tag == "Tassel")
{
Destroy (hit.transform.gameObject);
Instantiate (newTile, hit.transform.position, hit.transform.rotation);
}
}
}
I am not sure if I understood the question quite well.
You want your health to decrease with every movement?
If so, then start debugging your code, make sure it is going inside physics.raycasthit.
Also decrease your health not inside Physics.raycasthit, decrease it on keydown.
Eg:
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S))
{
health -= 1;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 25))
{
if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
{
transform.position.y -= 2.75;
}
}
}
What do you mean by "not working"? Is the character not moving? Is its health not decreasing? Are you detecting collisions?
To answer rmassanet, the variable and its value appear in the inspector, the problem is that that value should go down with each movement. The RayCast works fine as the character collides with what it is meant to, the script literally does everything it should except when the character moves the value in the inspector doesn't decrease like it should.
To answer ahaykal, yeah, so the player should hit the key, the character moves, and the health variable in the inspector decreases by one. Debugged the code, the raycasting works fine. Tried changing the placing of 'health -= 1;' and it had no effect.
ahaykal, I did that and it is decreasing the health with every movement just like I wanted it to, it didn't even occur to me that the inspector wouldn't be updating properly, thank you
Glad it worked out! Can you please accept the answer!
Answer by ahaykal · Jul 13, 2016 at 10:26 AM
Can you please print health instead of checking it out in the inspector, sometimes it doesn't update properly.
Your answer
![](https://koobas.hobune.stream/wayback/20220612084700im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is it possible to create a script dinamic like animation>size controling the Elements variables? 1 Answer
Javascript and C#, different behaviour in inspector ? 1 Answer
Why is my class hidden in inspector with C# and not JS? 1 Answer
Reading variable from other script 1 Answer
What is typed variable? 2 Answers