PowenKo, Unity Tutorial, physics , ball bounce

January 8, 2012   ·   0 Comments

Screen Shot 2012-01-07 at 11.05.35 PM

sample code: b45_1_physics_bounce

reference: http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html

http://forum.unity3d.com/threads/25804-The-perfect-bouncing-ball?p=793353#post793353

using UnityEngine;
using System.Collections;

public class ballBounce : MonoBehaviour {

private bool firstTimeVelocitySave = false;
private Vector3 savedVelocity;
public float BounceRate =1.5f;

	void OnCollisionEnter(Collision collision) {
		if (! firstTimeVelocitySave) {
			savedVelocity = rigidbody.velocity;
			firstTimeVelocitySave = true;
		}
		gameObject.rigidbody.velocity =new Vector3(0,savedVelocity.y,0);
		savedVelocity.y=savedVelocity.y/BounceRate;
	}

}

javascript

#pragma strict

var firstTimeVelocitySave = false;
var savedVelocity : Vector3;
var myRigidbody : Rigidbody;
var BounceRate:float =1.5;

function OnCollisionEnter(col : Collision) {
	//print("Collision! " + rigidbody.velocity);
	if (! firstTimeVelocitySave) {
		savedVelocity = rigidbody.velocity;
		firstTimeVelocitySave = true;
	}
	myRigidbody.velocity.y = savedVelocity.y;
	savedVelocity.y=savedVelocity.y/BounceRate;
}

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Unity3D4

Amazon

adsbygoogle

Recent Posts

Categories

Amazon