My stone(gameobject) falls, I’ve attached the rigid body and ticked gravity. Since the speed of gravity is too low for my game, I’ve used
var forceValue: float = 10f;
.....
rigidbody.AddForce(-Vector3.up * forceValue);
While falling, the stone needs to deviate from its path when left/right arrow keys are pressed. tried
transform.position.x--;
for left arrow, and
transform.position.x++;
for right arrow.
What am I actually looking for, is that when an arrow is pressed the stone needs to move to extreme edge of the screen smoothly/rapidly with out any change in the speed of down fall.
Precise positioning in physics is very complicated: you must write a PID controller to reach reasonably precise positions. Since your case is relatively simple - only X positioning - maybe a simple P (proportional) controller can do the job: you control the desired X coordinate with the keyboard (A or left arrow, D or right arrow) and the script tries to move the rigidbody there by controlling its velocity.x - an error is calculated and multiplied by a gain factor, and the result sets the X rigidbody velocity:
var xSpeed: float = 100; // control speed in x - units per second
var vGain: float = 10; // P controller gain
var maxVel: float = 100; // max P controller velocity
private var targetX: float;
function Start(){
targetX = transform.position.x; // initialize targetX
}
function Update(){
// move targetX coordinate with the Horizontal axis:
targetX += Input.GetAxis("Horizontal") * xSpeed * Time.deltaTime;
}
function FixedUpdate(){
// add your extra gravity
rigidbody.AddForce(-Vector3.up * forceValue);
// try to reach targetX by handling the rigidbody velocity.
// calculate the difference targetX - current X position:
var error = targetX - transform.position.x;
// set velocity.x proportionally to the difference (clamp to maxVel):
rigidbody.velocity.x = Mathf.Clamp(speed * error, -maxVel, maxVel);
}
You may have to tweak the parameters vGain and maxVel: if the rigidbody becomes too slow closer to the desired position, increase vGain; if it passes the final position and starts oscillating around it, reduce vGain; maxVel clamps the maximum velocity - reduce it if the rigidbody oscillates madly, and increase it if the rigidbody goes too slowly to the target point.
EDITED: Well, making it go to the left or right positions when the arrow keys are pressed and return to center when they are released is easy in this script: you must save the initial center X coordinate and define the max excursions to left and right, then change Update to move targetX to the desired position when the keys are pressed, and set it to the center when they are released. You must tweak the variables maxLeft and maxRight to make the object reach the desired left and right limits;
var maxLeft: float = 10; // max excursion to left - adjust in the Inspector
var maxRight: float = 10; // max excursion to right - adjust in the Inspector
var xSpeed: float = 100; // control speed in x - units per second
var vGain: float = 10; // P controller gain
var maxVel: float = 100; // max P controller velocity
private var centerX: float; // initial object X coordinate
private var targetX: float; // desired X coordinate
function Start(){
centerX = transform.position.x; // save center position in centerX...
targetX = centerX; // and initialize targetX
}
function Update(){
// move targetX with arrow keys:
if (Input.GetKey("left")){ // left arrow: move left
targetX = centerX - maxLeft;
}
else if (Input.GetKey("right"){ // right arrow: move right
targetX = centerX + maxRight;
}
else { // nothing pressed: return to center
targetX = centerX;
}
}
function FixedUpdate(){
// add your extra gravity
rigidbody.AddForce(-Vector3.up * forceValue);
// try to reach targetX by handling the rigidbody velocity.
// calculate the difference targetX - current X position:
var error = targetX - transform.position.x;
// set velocity.x proportionally to the difference (clamp to maxVel):
rigidbody.velocity.x = Mathf.Clamp(speed * error, -maxVel, maxVel);
}