- Home /
The question is answered, right answer was accepted
Objects passing through collision because of transform.translate
Hi,
I have an object that falls using rigidbody.AddForce not the default gravity.
And when you press left/right you move the object in order to navigate around obstacles.
My problem is that the player object will often pass through the wall objects i have even used waitForSecconds to temporary disable the controls but the problem still persists.
static var power : float = 0.0;
static var myY : float;
public var upperClampLimit : float; // can be changed form editor
public var lowerClampLimit : float; // can be changed from editor
public var fallAcceleration : float; // can be changed from editor
public var wallSlow : float; //can be changed from editor
public var movementShrinkerhoriz : float; // can be changed form editor
public var movementShrinkervert : float; // can be changed form editor
var shouldMove : boolean = true;
function Start(){
rigidbody.freezeRotation = true;
}
function Update (){
Debug.Log (shouldMove);
var horiz : float = Input.GetAxis("Horizontal");
var horizMovement = horiz/movementShrinkerhoriz;
if (shouldMove) {
transform.Translate(Vector3(horizMovement,0,0));
}
if (horizMovement!=0){
rigidbody.velocity.x = 0;
}
var vert : float = Input.GetAxis("Vertical");
var vertMovement = vert/movementShrinkervert;
power = power - vertMovement;
power = power +fallAcceleration;
if (power <=0){
power = power +fallAcceleration;
}
rigidbody.AddForce(Vector3(0,-power,0));
var dist = (transform.position - Camera.main.transform.position).z;
var upBorder = Camera.main.ViewportToWorldPoint(Vector3(0,upperClampLimit,dist)).y;
var downBorder = Camera.main.ViewportToWorldPoint(Vector3(0,lowerClampLimit,dist)).y;
transform.position.y = Mathf.Clamp(transform.position.y,upBorder,downBorder);
}
function FixedUpdate (){
myY = (rigidbody.position.y);
rigidbody.transform.position.z = 0;
}
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.name == "wall"){
shouldMove = false;
yield WaitForSeconds(0.5);
shouldMove = true;
if (power <=0){
power = power -wallSlow;
}
}
}
1 - format your code
2 - search on here for literally 100s of long expanations of this
3 - unityGE$$anonymous$$S.com for beginner articles
4 - Louis has generously given a good answer
Answer by Loius · Apr 07, 2013 at 07:03 PM
Use the 101-010 button to format code.
.Translate and .position are literally teleportation. If you need physics interactions, you have to use CharacterController.Move or .SimpleMove, or use Rigidbody.AddForce
/end copypasta
So to get your thingy to not go through walls, AddForce for the sideways movement as well.
How to make it behave like transform.translate? I need that specific behavior for my game to work. When i addForce the player slides too much, I don't know how to fix that. I know this is an old thread. :)
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Box collider with rigid body yet enemies still pass through each other. 1 Answer
2D: How to make Gradius III with a mouse? 1 Answer
Object passes through some walls? 1 Answer
Collision detection and prevention between a rigid body and box collider 0 Answers