- Home /
Rigidbody.AddForce equivalent of transform.Translate?
I making a game where players can alter their direction of gravity, allowing them to walk on walls and such. Currently the players' movement is altered using translate because that can apply movement based of the transform's local position. The problem here is that translate can force players through level geometry and I was hoping to utilise the rigidbody for movement to fix the problem, but this seems to apply to the character in worldspace, despite using TransformDirection, which means when the players walk on the roof, their input is reversed. Does anyone know of a good way to solve this problem?
EDIT:
This here is the relevant snippets of code, I haven't provide the whole script because it's about 200 lines and handles many things related to the player, but not specifically to the movement
void Update() {
moveVector = new Vector3(playerInst.currState.ThumbSticks.Left.X, 0, playerInst.currState.ThumbSticks.Left.Y);
moveVector *= moveSpeed;
moveVector *= Time.deltaTime;
rotateVector = new Vector3(0,playerInst.currState.ThumbSticks.Right.X, 0);
rotateVector *= rotateSpeed;
rotateVector *= Time.deltaTime;
}
void FixedUpdate() {
rigidbody.AddForce(-gravity * rigidbody.mass * myNormal);
myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed * Time.deltaTime);
var myForward = Vector3.Cross(transform.right, myNormal);
var targetRotation = Quaternion.LookRotation(myForward, myNormal);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpSpeed);
transform.Rotate(rotateVector);
transform.Translate(moveVector);
}
Answer by robertbu · Oct 18, 2014 at 12:39 AM
Without your code, it is difficult to be specific. But let's say that 'someMovement' is a Vector3 that you are feeding to your transform.Translate. Your Rigidbody equivalent could be:
Vector3 dir = transform.TransformDirection(someMovement);
Vector3 pos = transform.position + dir;
rigidbody.MovePosition(pos);
I gave this a try, but it doesn't fix the problem I'm having with player's passing through colliders. i really need to utilise addforce, or add relative force, but they are reversing the player's input when the player's is rotated onto a different surface.
Hey! I had the same problem with the rigidbody not moving in the local direction and you helped fix it! You're awesome!
Answer by Illuminism · Jul 04, 2016 at 04:39 PM
I know this is a 2 year old question but if anyone needs a way to Addforce to a rigidbody according to local position (So you can look around and move in the correct position) here it is in C#!:
This is assuming your variable movement is a Vector2, -1x = left , 1y = forward etc.
Play around with the speed until you move as much as you need. I used 20000 for my character.
Vector3 dir = transform.TransformDirection(movement);
myRigidbody.AddForce(dir * speed);
Answer by KpjComp · Oct 18, 2014 at 12:57 AM
An easy way to change gravity direction is not to actually change the gravity direction but instead put your whole scene inside a DummyObject and rotate this.
this wouldn't work because it's splitscreen multiplayer, rotating the whole world would mess everyone up.
Oh, right yes that would mess things up.. But it did get me thinking would they be a way to apply different gravity to each player. So I knocked this script up and it seems to work. If you apply this script to each player, assign a mod 0 and 1 to each one, and set there gravity vector eg. 0,-1,0 for one & 0,1,0 for the other, it has the effect of giving each a different gravity. It's a bit of a hack but it seems to work. It works by basically time/slicing the gravity to each player, so for x number of frames player 1 would say have gravity but player 2 would not, then the next x number of frames it's reversed. As a consequence gravity speed may be off, not really sure, but maybe this could be compensated with mass.
using UnityEngine;
using System.Collections;
public class gravatySet : $$anonymous$$onoBehaviour {
static int x = 0;
public Vector3 gravity;
public int mod;
void FixedUpdate() {
if ((x/10) % 2 == mod) {
rigidbody.useGravity = true;
Physics.gravity = gravity;
} else {
rigidbody.useGravity = false;
}
x += 1;
}
}
One slight problem with this approach all other objects in the scene's gravity would be effected, but this could be sorted by applying the script to those objects too, so you could time slice 3 ways, player1,player2,other objects.
Well, at least for me the gravity is not the issue, i've more or less got that part figured, the main issue is players' falling through floors, which can be fixed by move the players using the addforce, but i can't replicate the smooth movement I had using translate, especially as it applied relative to the player's local coordinates.
I'm new to Unity3d, but one thing I've noticed with there physics engine, if you mess directly with transforms it doesn't seem to play well. fyi: The example I've done here with above script does seem to play smooth, and the colliders work as expected. Anyway, glad you've nearly got it all figured out.
Your answer
Follow this Question
Related Questions
RigidBody clips into corners 3 Answers
Iphone move object slower than computer. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
climbing the hill? 1 Answer