Horje
Add Force or Velocity in Unity 2d on key press Code Example
Add Force or Velocity in Unity 2d on key press
void FixedUpdate()
{
    //Use Velocity
	if (Input.GetKey(KeyCode.D))
        {
        	//add velocity to rigidbody 3 units along x axis and 0 units along y axis
            //Velocity is not affected by Gravity
            //Velocity i constant in every frame I.e Force=3
            //Velocity starts from the initial value stated which in this case is 3
            //rigidbody2d.velocity = new vector2(x,y);
            //x is sideways and y is upwards
            
            rb2d.velocity = new Vector2(3,0);
            
            
           //To gradually increase Velocity in each frame see the line of code below
           
           rb2d.velocity += new Vector2(3,0);
        }
        
	//OR Use Force
	if (Input.GetKey(KeyCode.D))
        {
        	//Gradually adds Force/velocity to rigidbody 4 units to the right along x axis
            //Force is affected by gravity and Drag
            //Force starts from 0 and increases gradually by 4 units per frame
            rb2d.AddForce (Vector2.right * 4);
        }
}




Csharp

Related
c# get the return value of a func Code Example c# get the return value of a func Code Example
Xamarin Forms iOS Picker done Code Example Xamarin Forms iOS Picker done Code Example
c# exists in list Code Example c# exists in list Code Example
Garbage collect every 30 frames unity Code Example Garbage collect every 30 frames unity Code Example
download xml file asp.net web api Code Example download xml file asp.net web api Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8