- Home /
Augmenting a speed linked to a Vector 2
Hey Guys, I am making a game, and need some help. I have this player that will under some circunstances go down. When it goes down I want it to act like flappy birds, by having its speed augmenting every frame.
//Set the gravity
public Vector2 gravity = new Vector2 (0, -10);
//Makes the player fall while the space bar is not pressed
//How can I make it every frame go down faster?
if (Jumping == false)
{
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForce(gravity);
if (Input.GetKeyDown(KeyCode.Space)){
Jumping = true;
}
}
I am new to C# and Unity, so I really need some help and maybe an explication.
And how can I know my game FPS? How can I change it? Thanks.
this is a duplicate of a question you already asked. http://answers.unity3d.com/questions/898277/some-help-with-a-movement-code.html
You never replied to my last comment on that one(about the variable initialization), and then opened an identical question. Not cool Fritz.
Sorry Glurth. I read your answer there, but here I am making another question. There I was trying to make it work, here I asked something slightly different.
Sorry once again bud.
Answer by InvincibleCat · Feb 12, 2015 at 08:52 PM
public float VerticalAcceleration = -2.0f;
public float VerticalSpeedMax = 10.0f;
private Vector2 _currentSpeed = Vector2.zero;
and then, instead of your rigidbody code:
_currentSpeed.y += VerticalAcceleration * Time.deltaTime;
_currentSpeed.y = Mathf.Clamp(_currentSpeed.y, -VerticalSpeedMax, VerticalSpeedMax);
rigidbody2D.velocity = _currentSpeed * Time.deltaTime;
Cheers!
Got a couple of errors in line where the $$anonymous$$athf.Clamp works. First, shouldnt the -speedmax and speedmax be VerticalSpeed$$anonymous$$ax?
Second, I got these two errors. Will try to solve by miself, but maybe you can help me.
Assets/Scripts/Player$$anonymous$$ovement.cs(50,57): error CS1503: Argument #1' cannot convert
UnityEngine.Vector2' expression to type float' Assets/Scripts/Player$$anonymous$$ovement.cs(50,57): error CS1502: The best overloaded method match for
UnityEngine.$$anonymous$$athf.Clamp(float, float, float)' has some invalid arguments
I've updated my answer. Can You show me your lines?
public float VerticalAcceleration = -2.0f;
public float VerticalSpeed$$anonymous$$ax = 10.0f;
private Vector2 _currentSpeed = Vector2.zero;
if (Jumping == false) { _currentSpeed.y += VerticalAcceleration Time.deltaTime; _currentSpeed.y = $$anonymous$$athf.Clamp(_currentSpeed, -VerticalSpeed$$anonymous$$ax, VerticalSpeed$$anonymous$$ax); rigidbody2D.velocity = _currentSpeed Time.deltaTime; if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)){ Jumping = true; } }
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Texture flickering (sniper scope reticle) 0 Answers
Distribute terrain in zones 3 Answers
Button Enabling/Disabling using Collision Triggers? 1 Answer
2D platformer- getting errors I don't understand (c#) 1 Answer