- Home /
Diğer
2D How to move object only to the up
Hello I want to move object always to the up how can I do this can someone give me example code? (I'm beginner)
Answer by Artik2442 · Jun 11, 2020 at 08:05 AM
I don't know how you want to do it , so I will give you some example code:
transform.Translate(Vector3.up * speed * Time.deltaTime);
if you place this script in an update method, your gameObject will go indefinitely upwards.
But, if you want something that works with physics, you can use:
public Rigidbody2D rb2D;
void Start()
{
rb2D = GetComponent.<Rigidbody2D>();
}
void Update()
{
rb2D.velocity = Vector2.up * speed * Time.deltaTime;
}
You can notice that in each proposition, there is a speed value. You can create a public float speed
to set to whatever you want the speed at which your object will move upwards.
oops, it's transform.Translate(Vector2.up * speed * Time.deltaTime
and not transform.Translate(Vector3.up * speed * Time.deltaTime);
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
Most efficient/fastest way to rotate object around another object 0 Answers
Need help with char movement. 2 Answers
isGrounded is always false, even with gravity, how do you fix that? 1 Answer