- Home /
Same script, different properties to curtain objects
I am making a game where once the player or enemy interact with a tarpit tagged 'tar' they slow down to 1 speed. Both enemy and player use the same movement script. Once they leave the tar pit both go 3 speed. How do I get it so that the player goes back to 6 speed and the enemy goes to 3 speed.
Here is my code
public var walkingSpeed : float = 5.0;
function OnCollisionEnter(col: Collision){
if (col.gameObject.CompareTag("tar"))
{
walkingSpeed = 1.0;
}
}
function OnCollisionExit(col: Collision)
{
if (col.gameObject.CompareTag("tar"))
{
walkingSpeed = 3.0;
}
}
Post the part of the script that includes the variable declarations.
Answer by Seth-Bergman · Jan 01, 2013 at 06:23 AM
All you need to do is create a variable to store the relevant number:
public var walkingSpeed : float = 5.0;
public var myDefaultWalkSpeed : float;
function OnCollisionEnter(col: Collision){
if (col.gameObject.CompareTag("tar"))
{
walkingSpeed = 1.0;
}
}
function OnCollisionExit(col: Collision)
{
if (col.gameObject.CompareTag("tar"))
{
walkingSpeed = myDefaultWalkSpeed ;
}
}
then select each object and set the var to whatever you want
Your answer
Follow this Question
Related Questions
Enemy death help 1 Answer
Collsions problem 0 Answers
How can i make my scene restart once my character has collided with another object? 2 Answers
Decreasing Player Health On Collision with Enemy 2 Answers
Loading new level after killing enemies 3 Answers