Can you move a rigid body object forward without using relative force? If so, how?
I'm trying to make an object that moves along it's local Z axis but when I use AddRelativeForce it ends up sliding all over the place when I turn the object. I want a character to stop the second the walk key is released and to turn sharply, similar to games like Wizard 101. I'm using C#, here's my code so far:
using UnityEngine; using System.Collections;
public class character_controller_script : MonoBehaviour { public Rigidbody playerRB; public float playerWalkSpeed; public CapsuleCollider playerCapsuleColider; public float playerJumpPower;
void Start(){
playerRB = GetComponent<Rigidbody> ();
playerCapsuleColider = GetComponent<CapsuleCollider> ();
playerWalkSpeed = 8.0f;
}
void Update(){
float velocity = Input.GetAxis ("Vertical");
float rotation = Input.GetAxis ("Horizontal");
Vector3 movement = new Vector3 (0.0f, 0.0f, velocity);
playerRB.AddRelativeForce (playerWalkSpeed * movement); //This is what I need to get rid of, but how?
playerRB.transform.Rotate (Vector3.up * rotation);
}
}
I have the camera, the player object, and everything else inside the same game object so I don't need to worry about anything else right now, I just need to stop having the character drift.
!!!EDIT 9/4/2015!!!
I also need it to move forward and back on it's local axis, not it's global axis. The rotation is there to let the player turn, along with the camera, so don't worry about the character moving left and right, that isn't happening, just turning.
Answer by CaptainKirby · Sep 04, 2015 at 11:38 AM
Give Rigidbody velocity a go. It is a direct manipulation, so you dont get stuff like acceleratio given to you like with addforce etc. though. If you just want it to be straight up instant then it is a good choice.
inputDir.x = player.GetAxis("Move Horizontal");
inputDir.z = player.GetAxis("Move Vertical");
GetComponent<Rigidbody>().velocity = inputDir * speed;
I was just about to try this but whenever I type inputDir it shows up as an error. Do I need a new component? Or would a float work?
I forgot to mention that it needs to go on it's local axis, not the global axis, so velocity won't work. Thanks for the quick reply though.
in what axis it moves(local or glabal) makes no difference, it is simply a means of movement, like addforce and translate.
the inputDir is just a Vector3 variable.
However, for ease for you: Change this:
playerRB.AddRelativeForce (playerWalkSpeed * movement);
to:
playerRB.AddRelativeForce (playerWalkSpeed * movement, Forcemode.VelocityChange);
this will change the speed instantly. For more about forcemodes check out http://docs.unity3d.com/ScriptReference/Force$$anonymous$$ode.html
Also this thread has a more in depth explanation of the difference between addforce and velocity http://forum.unity3d.com/threads/addforce-vs-addrelativeforce-vs-rigidbody-velocity.32808/
You could use the axes of the object to direct it!
Vector3 inputDir = Vector3.zero;
inputDir.x += transform.right*player.GetAxis("$$anonymous$$ove Horizontal");
inputDir.z += transform.forward*player.GetAxis("$$anonymous$$ove Vertical");
playerRB.velocity = inputDir * speed;