- Home /
(Closed)I have a Issue with rigidbody and don't know how to fix it.
Hi there. I'm kind of new to unity and I want to learn scripting using it. But at this point I have a issue and I don't know why it is happening nor how to fix it. Here is the deal. I'm using rigidbody forces ("AddRelativeForce") to move the player. The problem is that when the player is looking to "Z" axis the player moves to "Z" axis, when he is looking to "-X" axis he moves to "-X" axis but when the player looks between the "Z" and "-X" he doesn't moves to that direction, he moves slightly different way.
Here is the problem (sorry for my bad paint skills xD).

Here is the script that I have in the project:
using UnityEngine;
using System.Collections;
public class PlayerMovementeScript : MonoBehaviour
{
private Rigidbody rb;
private float movementeSpeed;
private float maxMovementeSpeed;
private float xRotation;
private float yRotation;
private float rotationSpeed;
private GameObject mainCameraObject;
private Vector2 speedController;
void Start ()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
mainCameraObject = GameObject.Find("Main Camera");
rotationSpeed = 15f;
maxMovementeSpeed = 2f;
movementeSpeed = 100f;
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
MovementeSpeedController();
PlayerCameraController();
}
private void MovementeSpeedController()
{
//speedController is the horizontal movemente of the player so he can controle the speed
speedController = new Vector2(rb.velocity.x, rb.velocity.y);
//Checking if speed is higher than max speed allowed
if(speedController.magnitude > maxMovementeSpeed)
{
speedController = speedController.normalized;
speedController *= maxMovementeSpeed;
}
//Add force to the player
rb.AddRelativeForce(Input.GetAxis("Horizontal") * movementeSpeed, 0, Input.GetAxis("Vertical") * movementeSpeed);
//Set the player speed to control it
rb.velocity = new Vector3(speedController.x, rb.velocity.y, speedController.y);
//Setting the run speed or walk speed
if(Input.GetKey(KeyCode.LeftShift))
{
maxMovementeSpeed = 5f;
}
else
{
maxMovementeSpeed = 2f;
}
}
private void PlayerCameraController()
{
xRotation += Input.GetAxis("Mouse X") * Time.deltaTime * rotationSpeed;
yRotation -= Input.GetAxis("Mouse Y") * Time.deltaTime * rotationSpeed;
//Limiting yRotation so the player doesn't flips his neck xD
yRotation = Mathf.Clamp(yRotation, -90, 90);
mainCameraObject.transform.rotation = Quaternion.Euler(yRotation, xRotation, 0);
rb.rotation = Quaternion.Euler(0, mainCameraObject.transform.rotation.eulerAngles.y, 0);
}
}
Sorry for my bad English :s and thank you.
Are you trying to make it move forward? If so there is another option that is quite possibly simpler than the RelativeForce.
I forgot to mention, it is a FPS game. And yes, I'm trying to make him move forward.
Answer by Zinatulin · May 03, 2015 at 12:11 AM
I've found the issue, it is on line 42:
speedController = new Vector2(rb.velocity.x, rb.velocity.y);
I should have:
speedController = new Vector2(rb.velocity.x, rb.velocity.z);
But thank you anyway for your time :)
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Stacking physics objects? 1 Answer
Jumping with rigid body3d 1 Answer
how to keep a "ship" over a platform or rigidbody? 0 Answers