- Home /
How to move the ball according to the camera direction?
Hi. I'm trying to make 3rd person ball that the user moves with arrows and with the mouse the user controls the camera(which orbits around the ball). This is my script for the ball:
public class BallMovement : MonoBehaviour {
// Use this for initialization
public Camera MyCamera;
public GUIText Ind;
bool OnFloor = true;
private float x = 0.0f;
void Update()
{
RaycastHit RHit;
Ray MyRay = new Ray(transform.position, Vector3.down);
Debug.DrawRay(transform.position, Vector3.down*3);
if (Physics.Raycast(MyRay, out RHit, 4))
{
if (RHit.collider.gameObject.tag == "Floor")
{
OnFloor = true;
Ind.text = "On the Floor";
}
}
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxisRaw("Horizontal");
float moveVerticle = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVerticle);
rigidbody.AddForce(movement * 30.0f);
//MyCamera.transform.position = new Vector3(transform.position.x - 2, transform.position.y+27, transform.position.z - 75);
if (Input.GetButtonUp("Jump") && OnFloor==true)
{
rigidbody.AddForce(new Vector3(0,50,0)*5.0f);
OnFloor=false;
Ind.text = "Jumping";
}
}
}
and for my camera I'm using the existing script in the standard assets folder/mouse orbit script.but my problem is that when i turn the camera with mouse and press the up arrow i want the ball to move towards the direction of the camera is looking.but it doesn't do that now with my script. can anyone help?
===================================
The Answer : this is my code for moving the ball according to answers:
float moveHorizontal = Input.GetAxisRaw("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = MyCamera.transform.forward * moveVertical*speed*Time.deltaTime ;
rigidbody.AddForce(movement);
movement = MyCamera.transform.right * moveHorizontal*speed*Time.deltaTime;
rigidbody.AddForce(movement);
Answer by EvilTak · Dec 07, 2014 at 02:49 AM
You can do this:
float moveVertical = Input.GetAxisRaw("Vertical");
Vector3 movement = mainCamera.transform.forward * movementVertical * 30;
rigidbody.AddForce(movement);
Before this, you will have to create public Camera mainCamera, and in the inspector assign it to your main camera which has the mouse orbit script. If you want to do the same for horizontal axes use mainCamera.transform.right if right arrow has the positive value in your axis else use negative of the horizontal axis value.
Thanks.the Vertical movement now is ok(except that the ball doesn't move softly on the terrain.the ball mass is 0.7).about the X axis you mean like this? : float moveHorizontal = Input.GetAxisRaw("Horizontal"); if(moveHorizontal >0) { i don't know the rest} could you explain? thank you
In your horizontal input axis, mostly the right direction will be positive, so Vector3 movement = mainCamera.transform.right * movementHorizontal * 30
. If on playing the ball moves in the opposite direction that is left when you press right, just use mainCamera.transform.left ins$$anonymous$$d of mainCamera.transform.right. If that helped, please accept this answer.
thanks.but i don't still get it how can i have two vector3 movement?
Just reuse the vector movement. No need to define it again.
i know this is a beginner question but how can i combine vertical and horizontal(right) without using another vector3 (which doesn't work). You wrote that i am supposed to reuse the vector movement but i cant figure it out. An example would be highly appreciated.