- Home /
Other
Using Camera Y Rotation to Determine Player Movement Direction
I am looking looking to use rigidbody.addforce to push a ball (player) around the board. I am using stock the Mouse Orbit on my main camera and the control script show below on my player.
The issue I am encountering is the when I use transform.TransformDirection(movement), it is using the rotation of the camera around the X axis as well as the Y axis. This is causing the force being applied to push the ball slightly down and forward rather than just forward. This results in the forward movement of the ball being slower and the other directions (Strafing and Reverse).
I am looking for a way to only use the rotation of the Camera around the Y axis so that it applies the force in just the forward direction and not down. I have looked for something that can do this but have had no luck.
Any help is appreciated. Thanks!
using UnityEngine; using System.Collections;
public class Player_Controller : MonoBehaviour { private Rigidbody rb; private float speed;
public float jump;
private bool canJump;
public Transform playerCamera;
void Start()
{
rb = GetComponent<Rigidbody> ();
}
void OnCollisionEnter (Collision collision)
{
canJump = true;
speed = 400;
}
void FixedUpdate ()
{
if (canJump == true)
{
if (Input.GetKeyDown (KeyCode.Space))
{
rb.AddForce (new Vector3 (0, jump, 0));
speed = 200;
canJump = false;
}
}
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
movement = playerCamera.transform.TransformDirection(movement);
rb.AddForce (movement * speed * Time.deltaTime);
}
}
Follow this Question
Related Questions
Creating a file that records the player position during a game in xyz coordinates 0 Answers
Camera relative rigidbody third person player movement 1 Answer
Rigidbody(.addForce) doesn't work with Physic material 1 Answer
I'm a bit new but when my camera rotates with the player model to look around it kind of jitters 0 Answers
Stop and object from moving faster after a specific speed is reached? 2 Answers