How can physics material game object maintain a constant velocity?

1088381--40824--$physic material.jpgHi :slight_smile:

My programing skills are rusty and I am new to Unity. Any help will be greatly appreciated :slight_smile:

I have a game object called ball that has a physic material applied to it. I have attached an image of the physics properties. Basically
Bounciness set to 1
Static and dynamic friction set to 0
Friction Combine set to minimum
Bounce combine set to maximum

Its just a breakout clone game.
The ball is a game object with a collider that bounces of the screen. Sometimes when it collides with the paddle it will sometime change its velocity.

FixedUpdate() currently has no code. The ball is moving based only on the physics material that is attached to it.

I know that if I have no physics material then i can call the following code to move the ball.
// this.movementX = this.moveDirX * this.moveSpeed;
// this.movementY = this.moveDirY * this.moveSpeed;
// this.movementX *= Time.deltaTime;
// this.movementY *= Time.deltaTime;
// this.thisTransform.Translate(this.movementX, this.movementY, 0.0f);

However, I don’t know how to incorporate that code when there is a physics material on the object.
My objectives are the following…
I want to have a variable that stores the ball’s velocity.
I want to make sure I can maintain a constant velocity on the ball.
I want it to double it’s velocity or change it’s velocity after it collides with the “Speed Power Up” or “Slow Power Up”

Any help would be greatly appreciated.

Thank You :slight_smile:

Hi humbled,

You would use rigidbody.velocity = new Vector3(x,x,x) to set the velocity of the balls explicitly.

rigidbody.velocity *= 2f; // Twice speed;
rigidbody.velocity *= 0.5f // Back to normal (if the above has been done before hand).

I am a fan of VelocityChange. It takes a bit longer to understand fully, but I like it.

#pragma strict
public var speed:float = 5.0;
private var wantVelocity:Vector3 = Vector3.zero;
function Start(){
	var xSpeed:float = Random.Range(-speed*.5, speed*.5);
	wantVelocity = Vector3(xSpeed,0 , (xSpeed < 0) ? Random.Range(-xSpeed*2, 1.0) * speed : Random.Range(xSpeed*2, 1.0) * speed);
}

function FixedUpdate(){
	var deltaVel:Vector3 = ((wantVelocity.normalized)*speed) - rigidbody.velocity;
	rigidbody.AddForce(deltaVel, ForceMode.VelocityChange);
}

function OnCollisionEnter(c:Collision){
	for(var ctc:ContactPoint in c.contacts){
		switch(ctc.otherCollider.tag){
			case "Wall":
				wantVelocity.x = -wantVelocity.x;
				break;
			
			case "Top":
				wantVelocity.z = -speed;
				break;
			case "Paddle":
				// handle side of paddle here...
				
				if(ctc.point.z < transform.position.z){
					wantVelocity.z = speed;
				}
				break;
		}
	}
}

Basically, whatever force it needs to apply to get to your wanted speed, it will apply.

If you want to effect speed changes, do something like:

baseSpeed = 5.0;
boostSpeed = 7.5;
slowSpeed = 2.5;

if slowed… speed = slowSpeed;
if boost… speed = boostSpeed;
if normal… speed = baseSpeed;

You probably would have an easier time skipping the physics engine but either way, a distinction you will want to make is that it’s the rigidbody component that makes the physics engine aware of GameObjects. You don’t need a physics material - they allow you to tailor collisions. Don’t forget Drag (and angularDrag).

thank you everyone. you all gave me good advice :slight_smile: