Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JoshuaHodkinson · Oct 18, 2014 at 12:36 AM · c#rigidbodytranslate

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);    
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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);

 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoshuaHodkinson · Oct 18, 2014 at 01:48 AM 0
Share

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.

avatar image Illuminism · Jul 04, 2016 at 04:26 PM 0
Share

Hey! I had the same problem with the rigidbody not moving in the local direction and you helped fix it! You're awesome!

avatar image
1

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoshuaHodkinson · Oct 18, 2014 at 01:02 AM 0
Share

this wouldn't work because it's splitscreen multiplayer, rotating the whole world would mess everyone up.

avatar image KpjComp · Oct 18, 2014 at 01:43 AM 0
Share

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.

avatar image JoshuaHodkinson · Oct 18, 2014 at 01:45 AM 0
Share

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.

avatar image KpjComp · Oct 18, 2014 at 01:57 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges