I'm currently setting the rotation of a player object so that it "stands up" relative to the spheroid the user stands on.
However, that will break Rigidbody physics.
I already have the direction of gravity as a Quaternion, how can add torque to the Rigidbody so that it aligns with that direction?
Here's my code on Github, which does allow the player to move around the surface of the planet.
I have tried various approaches using AddTorque and Vector3.Cross, but they have all only ended in the Rigidbody spinning wildly.
Instead of using transform whenever a Rigidbody is involved rather first calculate the needed final Quaternion ad then apply it using theRigidbody.MoveRotation which does not break the physics.
The same also accounts to the position where you should rather use theRigidbody.MovePosition.
Both should be used in FixedUpdate so
either move the entire code from Update to FixedUpdate
or split the input and the physics and do your API stuff in Update, store the relevant values into fields but apply anything related to the Rigidbod transformations reactive in FixedUpdate.
Your code is quite complex so I will only give an example as a pseudo code and hope you can take it from there
So instead of doing e.g.
private void Update ()
{
player.transform.position = XYZ;
// Applies a world space rotation
player.transform.rotation = XYZW;
// Adds a relative rotation in local space
player.transform.Rotate(x,y,z);
}
You would rather do something like
[SerializeField] Rigidbody playerRb;
private Vector3 targetPosition;
private Quaternion targetRotation;
private void Awake ()
{
if(!playerRb) playerRb = player.GetComponent<Rigidbody>();
}
private void Update ()
{
targetPosition = XYZ;
targetRotation = XYZW * Quaternion.Euler(x,y,z);
}
private void FixedUpdate ()
{
playerRb.MovePosition(targetPosition);
playerRb.MoveRotation(targetRotation);
}
Related
Stickman
I have this stickman here in unity 2D and i want his arms to always reach out to the direction of the mouse. I have no ideas on where i should start and i'm hoping someone here could help me out. Thanks!
Foremost, you need to determine the mouse position. It can be done with the following method:
public Vector3 GetMouseWorldPosition()
{
Vector3 mouseScreenPosition = UnityEngine.Input.mousePosition;
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(mouseScreenPosition);
return mouseWorldPosition;
}
I use Camera.main here for demonstration purposes. In sake of performance it will be better to cache it as a field in your class.
Now, when you have the target coordinates, you can rotate the hand. Here is an example method:
public void RotateLimb()
{
Vector3 rotationTargetPosition = GetMouseWorldPosition();
Vector3 directionVector = (rotationTargetPosition - _limb.transform.position).normalized;
float angleInRad = Mathf.Atan2(directionVector.y, directionVector.x);
Vector3 targetAngle = _limb.transform.eulerAngles;
targetAngle.z = angleInRad * Mathf.Rad2Deg;
_limb.rotation = Quaternion.Euler(targetAngle);
}
Method RotateLimb() can be called in Update or a coroutine.
All the variables of this method can also be stored as private fields in your class.
_limb must contain Transform of your hand GameObject and you can assign it as SerializeField
[SerializeField] private Transform _limb;
_limb will rotate around its pivot. Therefore, pivot must be in the center of the shoulder. In order to achieve it, you can place all the graphics of your hand as a child of _limb and adjust it accordingly.
This seems like the most straightforward way to do it.
The player goes on top of some objects when he walks toward them, how can I prevent that from happening? Here is an example image of that:
I did not jump to be on the couch but yet it still goes on top of it when I walk to it. Here is my player information:
I don't want to change the player's movement, but I don't want it to go on top of objects when I'm walking.
Make the Step Offset in character controller 0. More info on it here
The character controller Step Offset solves this problem by increasing it value, but you may find that after adding the value, the character controller does not generate any gravitational force on its own.
To solve the problem of gravity, it is enough to first get the component.
private CharacterController controller;
public void Start()
{
controller = GetComponent<CharacterController>();
}
And then apply gravity to the object with the following instructions, you have already obtained the moveInput axis with the Input.GetAxis method.
private Vector3 velocity;
private void Update()
{
var moveInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
// === AFTER CALCULATING MOVE INPUT
controller.Move(moveInput*Time.deltaTime);
velocity += Physics.gravity * Time.deltaTime;
controller.Move(velocity); // Apply Gravity
if (controller.isGrounded) velocity = Vector3.zero;
}
After rotating the character with jotstick. rotate resetting. direction of the character With the joystick, I want the character to look in that direction when I turn my hand in the direction I want and then pull my hand out of the joystick. Don't let him look in the same direction joysticki when I left. how can I do it. thanks.
public class MyJoystick : MonoBehaviour
{
public Joystick joystick;
public Joystick joystickRot;
public float moveSpeed;
Quaternion targetRotation;
Rigidbody rigidbody;
// Start is called before the first frame update
void Start()
{
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// var rigidbody = GetComponent<Rigidbody>();
rigidbody.velocity = new Vector3(joystick.Horizontal * moveSpeed, rigidbody.velocity.y, joystick.Vertical * moveSpeed);
// this is problem
// don't reset the rotate when joysticki is released.
transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Atan2(joystickRot.Horizontal
, joystickRot.Vertical) * Mathf.Rad2Deg, transform.eulerAngles.z);
}
}
'joystickRot' will return zeros when not being pushed in any other direction, that is why your rotation is being reset, so you must check first that the joystick is actually being used before applying the values to your GameObjects transform rotation.
I'm not overly familiar with the joystick system you are using, but there will most certainly be a way to test if the joystick is being used or not, and only apply the force and rotations when it is.
By the way, you have two Joystick objects referenced, but I believe you only need one.
I attach a rigidbody to a HingeJoint in Unity.
It's attached no at the the center of an anchor of a joint.
To stop it from falling I set Use Motor = true, Target Velocity = 0 and apply some force to the motor.
However, this does not help, see video:
http://g.recordit.co/JJ0cmfT1Mb.gif
Here is the sample project: https://drive.google.com/open?id=0B8QGeF3SuAgTNnozX05wTVJGZHc
Screen to reproduce the issue (there is no code):
How can I apply a friction to a joint to stop rigidbody from moving?
Setting spring with damper or applying a limit is not an option for me, because I need to rotate the rigidbody correctly when I apply enough torque (force) to a motor.
If I switch damper/limit on and off, the rigidbody will move faster when it is rotated in a direction of falling and slower when it is rotated in opposite direction.
I've put together a behaviour that applies friction on a hinge joint. Here it is:
using UnityEngine;
public class JointFriction : MonoBehaviour {
[Tooltip("mulitiplier for the angular velocity for the torque to apply.")]
public float Friction = 0.4f;
private HingeJoint _hinge;
private Rigidbody _thisBody;
private Rigidbody _connectedBody;
private Vector3 _axis; //local space
// Use this for initialization
void Start () {
_hinge = GetComponent<HingeJoint>();
_connectedBody = _hinge.connectedBody;
_axis = _hinge.axis;
_thisBody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
var angularV = _hinge.velocity;
//Debug.Log("angularV " + angularV);
var worldAxis = transform.TransformVector(_axis);
var worldTorque = Friction * angularV * worldAxis;
_thisBody.AddTorque(-worldTorque);
_connectedBody.AddTorque(worldTorque);
}
}
This answers the headline of your question, and is what I was looking for when I found it. However this probably won't work for what you want from your more detailed description.
This applies friction that is proportional to the angular velocity, if there's no angular velocity, no force is applied. This means that under a constant force, like gravity in your example, it will always move at some reduced speed. In reality, imperfections in materials and other real world messiness mean that applying friction to joints can keep them still against a constant torque, but in a game engine, you will need some other torque to counter your constant force.
It sounds like you don't want gravity. You can just turn gravity off in the rigid body's inspector.
I make a character move on the surface of a circle. I let the camera move and rotate follow character. But the camera move and rotate very jerky. If I increase the value of the third parameter, the shock increases. and to reduce the value of the third parameter, the camera does not rotate to keep up the character. Help me fix it
My Code Camera Follow Player
public class CameraFollow : MonoBehaviour
{
public Transform player;
GameController gc;
public float speed = 2;
Vector3 pos = new Vector3 (0, 0, -10);
// Use this for initialization
void Start ()
{
gc = FindObjectOfType (typeof(GameController)) as GameController;
}
void FixedUpdate ()
{
if (gc.gameState == GameController.GameState.playing || gc.gameState == GameController.GameState.changeWave) {
transform.position = player.position + pos;
transform.rotation = Quaternion.Slerp (transform.rotation,
player.transform.rotation,
speed * Time.deltaTime);
}
}
}
Setting the position of a transform inside of FixedUpdate is a red flag for sure, especially when you're reporting that it's "jerky". Fixed update happens at an irregular interval compared to the frames displayed. This is because Physics needs to update using a fixed time step. The reason why this is the case is out of scope for this question.
Long story short, try changing FixedUpdate to Update and that should fix things looking "jerky".
Let me know if this doesn't work and I'll look for other possible causes.
If you are using a Rigidbody2D to move the character, make sure to set its Interpolate property to 'Interpolate'. This should fix it.