- Home /
Need help with a "roll" script (C#)
I'm trying to make a simple 3rd person RPG and want the character to be able to do a roll to dodge. Right now it just makes it impossible to walk in the opposite direction after hitting Shift-W. My current code is this: public class Roll : MonoBehaviour {
public float testStamina = 100.0f;
public float coolDown = 2.0f;
public float rollLength = 5.0f;
public float lastRoll;
public float rollDuration = 2.0f;
public float startTime;
bool canRoll;
// Use this for initialization
void Start () {
lastRoll = Time.time;
canRoll = true;
}
// Update is called once per frame
void Update () {
if(coolDown >= Time.time - lastRoll)
canRoll = true;
if(Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift) && canRoll == true){
canRoll = false;
startTime = Time.time;
if(startTime - rollDuration <= Time.time){
rigidbody.AddForce(Vector3.forward * -100f);
}
lastRoll = Time.time;
}
}
}
I feel like its something I should really know how to do, but its 2 in the morning and I need to finish this. Any help is greatly appreciated.
Answer by tw1st3d · Jul 24, 2013 at 03:52 AM
public class Roll : MonoBehaviour {
public float testStamina = 100.0f, coolDown = 2.0f, rollLength = 5.0f, lastRoll, rollDuration = 2.0f, startTime;
public bool canRoll;
void Start()
{
lastRoll = Time.time;
canRoll = true;
}
void Update()
{
if(coolDown >= (Time.time - lastRoll))
canRoll = true;
if((Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift)) && canRoll == true)
{
canRoll = false;
startTime = Time.time;
if((startTime - rollDuration) <= Time.time)
rigidbody.AddForce(Vector3.forward * -100f);
lastRoll = Time.time;
}
}
}
Your conditional grouping isn't correct.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110728im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adding force to a 2d object in specified direction not working? 0 Answers
transform.position not working in OnControllerColliderHit 2 Answers
Why is Unity always adding some strange Transformations? 0 Answers
create a cannon ball based on cannon angles and cannon power 0 Answers
Best way to move a Rigidbody2D from point A to point B (Mouse Position) 3 Answers