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);