Beginner here, so basically when I try to add force to an object, it moves slightly off center, as seen in the video below. I have tried using .AddForce(transform.forward), and .AddRelativeForce(Vector3.forward) but they both yield the same result. Any help would be appreciated. My code related to the object is here:
public class ThrowThrowable : MonoBehaviour
{
public Rigidbody throwableRb;
PlayerManager playerManager;
[SerializeField] float forceStrength;
void Start()
{
throwableRb = gameObject.GetComponent<Rigidbody>();
playerManager = GameObject.Find("Player").GetComponent<PlayerManager>();
}
void Update()
{
}
private void LateUpdate()
{
if (playerManager.throwable != null && playerManager.eIndex == 1)
{
throwableRb.AddRelativeForce(Vector3.forward, ForceMode.Impulse);
}
}
}
public class PlayerManager : MonoBehaviour
{
Rigidbody playerRb;
GameObject playerCamera;
public GameObject playerVisual;
public GameObject throwable;
GameObject availableThrowable = null;
[SerializeField] GameObject boxPrefab;
float horizontalInput;
float verticalInput;
Vector3 verticalMovement;
Vector3 horizontalMovement;
[SerializeField] float playerSpeed;
[SerializeField] float jumpHeight;
[SerializeField] float mouseSensitivity;
float mouseInputX;
float mouseInputY;
bool grounded;
public int eIndex;
void Start()
{
grounded = true;
playerRb = GetComponent<Rigidbody>();
playerCamera = GameObject.Find("Main Camera");
playerVisual = GameObject.Find("Player Visual");
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
PlayerMovement();
mouseInputX += Input.GetAxis("Mouse X") * mouseSensitivity;
mouseInputY += Input.GetAxis("Mouse Y") * mouseSensitivity;
playerCamera.transform.rotation = Quaternion.Euler(-mouseInputY, mouseInputX, 0f);
playerVisual.transform.rotation = Quaternion.Euler(0f, mouseInputX, 0f);
if (Input.GetKeyDown(KeyCode.E) && availableThrowable != null)
{
eIndex++;
throwable = availableThrowable;
}
eIndex = eIndex % 2;
Debug.Log(eIndex);
}
private void LateUpdate()
{
if (throwable != null && eIndex == 1)
{
throwable.transform.position = transform.position + playerCamera.transform.forward * 3f + transform.up * 2f;
throwable.transform.rotation = playerCamera.transform.rotation;
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Floor"))
{
grounded = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Throwable"))
{
availableThrowable = other.gameObject;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Throwable"))
{
availableThrowable = null;
}
}
void PlayerMovement()
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
horizontalMovement = playerVisual.transform.forward.normalized * verticalInput * playerSpeed * Time.deltaTime;
verticalMovement = playerVisual.transform.right.normalized * horizontalInput * playerSpeed * Time.deltaTime;
playerRb.MovePosition(transform.position += horizontalMovement);
playerRb.MovePosition(transform.position += verticalMovement);
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
playerRb.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
grounded = false;
}
}
}
And the video is available here: