hi
can anybody help me !
i want to make a simple game
that hit or flick a marbel in it
but the code that i see in the web and tryed it ,seem like applying force to ball like bowling and not like applying an instantaneous to the ball!
this code is for bowling that i say:
#pragma strict
var mousepowerx:float = 100.0;
var mousepowery:float = 100.0;
//var maxVelocitySquared:float=400.0;
private var forcey:float=0.0;
private var forcex:float=0.0;
private var ready:int=0;
//private var floorTag:String = "Floor";
function OnMouseOver(){
if( Input.GetMouseButton(0) && ready==0){
//rigidbody.AddForce(forcex*300,0,forcey*300);
ready=1;
}
}
function FixedUpdate() {
forcex = mousepowerx*Input.GetAxis("Mouse X")/Time.deltaTime;
forcey = mousepowery*Input.GetAxis("Mouse Y")/Time.deltaTime;
if( Input.GetMouseButton(0)==false && ready==1){
ready=2;
}
if(ready==2)
{//forcex = mousepowerx*Input.GetAxis("Mouse X")/Time.deltaTime;
//forcey = mousepowery*Input.GetAxis("Mouse Y")/Time.deltaTime;
rigidbody.AddForce(forcex*300,0,forcey*300);
ready=0;
}
}
…i want to make like this game):
please help if you can (:
I usually prefer to set rigidbody.velocity directly instead of applying forces in such cases (shooting a cannonball, kicking a soccer ball, throwing a marble etc.). Calculate a vector that points in the desired direction and multiply it by the velocity you want: the rigidbody will be launched in that direction exactly like expected.
Supposing that you want to throw the marble in the direction the camera is looking, and at a 30 degrees elevation angle:
public var vel: float = 5;
public var angle: float = 30;
function ThrowMarble(marble: GameObject){
var dir: Vector3 = Camera.main.transform.forward; // get the camera direction
dir.y = 0; // make it strictly horizontal
// adjust dir to the elevation angle:
dir = dir.normalized * Mathf.Cos(angle * Mathf.Deg2Rad); // adjust X and Z
dir.y = Mathf.Sin(angle * Mathf.Deg2Rad); // adjust Y
// dir now points in the right direction and has length 1:
// calculate velocity and assign to the rigidbody
marble.rigidbody.velocity = dir * vel;
}
If I understand correctly you want to be able to “flick” an object at position, giving a force and torque. Using AddForceAtPosition you’ll be able to do just that. Here’s a basic example, attach the script to an object in your scene - for instance the Main Camera. The script will shoot a raycast into the scene on left mouse press, if it hits an object with a rigidbody component it will add a force and torque to it from hit position.
#pragma strict
var force : float = 10.0;
function Update () {
// If left mouse button is pressed
if (Input.GetMouseButtonDown(0)) {
// Create a Raycast from camera view
var hit : RaycastHit = InteractionRaycast();
// If RaycastHit has a rigidbody
if (hit.rigidbody) {
// Apply force at position
ApplyForceAtPosition(hit.rigidbody, hit.point);
}
}
}
function InteractionRaycast () : RaycastHit {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, hit);
return hit;
}
function ApplyForceAtPosition (body : Rigidbody, position : Vector3) {
var direction : Vector3 = body.transform.position - position;
body.AddForceAtPosition(direction.normalized*force, position, ForceMode.Impulse);
}