Problem with ball following camera direction.

So I am making this ball type game.

If you could play this real fast and notice my problem: http://dl.dropbox.com/u/20233101/WM/%2Cm/WebPlayer/WebPlayer.html

The problem is that the ball does not follow the direction the camera is pointed, you can only use the “WASD” and rotate around it. I want it like the “FPS camera” where you look and you can walk in the direction.

Basically you are looking to the left and you still push “W” and it still moves forward, instead of to the left. I need it to “look left then go and travel left”… it’s much easier to see what I’m talking about when you play it, you can clearly see the problem :stuck_out_tongue:

Here is the Ball’s movement script:

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour 
{
	private GameObject _GameManager;
	public Vector3 movement;
	public float moveSpeed = 6.0f;
	public float drag = 2;
	
	void Start()
	{
		_GameManager = GameObject.Find("_GameManager");
	}
	
	void Update () 
	{	
		Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
		forward.y = 0;
		forward = forward.normalized;
		
		Vector3 forwardForce = forward * Input.GetAxis("Vertical") * moveSpeed;
		rigidbody.AddForce(forwardForce);
		
		Vector3 right= Camera.main.transform.TransformDirection(Vector3.right);
		right.y = 0;
		right = right.normalized;

		Vector3 rightForce= right * Input.GetAxis("Horizontal") * moveSpeed;
		rigidbody.AddForce(rightForce);
	}
	
	void OnTriggerEnter  (Collider other  ) 
	{
		if (other.tag == "Destroy")
		{
			_GameManager.GetComponent<GameManager>().Death();
			Destroy(gameObject);
		}
		else if (other.tag == "Coin")
		{
			Destroy(other.gameObject);
			_GameManager.GetComponent<GameManager>().FoundCoin();
		}
		else if (other.tag == "SpeedBooster")
		{
			movement = new Vector3(0,0,0);
			_GameManager.GetComponent<GameManager>().SpeedBooster();
		}
		else if (other.tag == "JumpBooster")
		{
			movement = new Vector3(0,0,0);
			_GameManager.GetComponent<GameManager>().JumpBooster();
		}
		else if (other.tag == "Teleporter")
		{
			movement = new Vector3(0,0,0);
			_GameManager.GetComponent<GameManager>().Teleporter();
		}
    }
}

And for the camera I have a basic “SmoothLookAt” and “MouseOrbit” script.

If anyone could tell me how to fix this, it would be really great!

Thanks,
Wade

Hmmm, I’m having a lot of trouble following your script to be honest, because your way of thinking is different to mine.

But isn’t the force you’re adding relative to the world co-ordinates rather then your camera co-ordinates? I’d recommend using the rigidbody.velocity rather than addforce.

EDIT: Unless there is a way to use the addrelativeforce and make it relative to your camera rather than your ball.

I see… I will try it!

If it does not work I will come back. :stuck_out_tongue:

make sure you change the line of code to rigidbody.velocity += instead of =

What line am I supposed to change though? :stuck_out_tongue:

This is the “Roll a Ball” asset from the store.

Just these two?

rigidbody.AddForce(forwardForce);

To

rigidbody.velocity +=(forwardForce);
rigidbody.AddForce(rightForce);

To

rigidbody.velocity +=(rightForce);

Also, if it helps at all, this is what I’m trying to go for:

(I need to add the “Jump” in there too…) But basically you turn the camera in the direction you want to roll your ball, along with using the arrows.

Yeah, those are the two you want to change.

I just finished coding the script yesterday, your looking for myself.

It’s a fair amount different than your’s but it works perfectly :slight_smile:

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {
	private Vector3 camForward;
	private Vector3 camRight;
	private bool grounded;
	
	public float speed = 10;
	public float jumpHeight;
	public float gravity = -1;
	public float maxVelocity;
	
	void OnCollisionStay(){
		grounded = true;
	}
	
	void OnCollisionExit(){
		grounded = false;
	}

	// Use this for initialization
	void Start () {
		 Physics.gravity = new Vector3(0, gravity * -1, 0);
	
	}
	
	// Update is called once per frame
	void Update () {	
		rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxVelocity);
		print(rigidbody.velocity);
		
		//Find the forward direction of the camera and set the y axis to 0
		camRight = Camera.main.transform.TransformDirection(Vector3.right);
		camForward = Camera.main.transform.TransformDirection(Vector3.forward);
		camForward.y = 0;
		camRight.y = 0;
		
		//Move the character forward/backward
		if(Input.GetKey(KeyCode.W)){
		   rigidbody.velocity += camForward * speed * Time.deltaTime;
		}
		if(Input.GetKey(KeyCode.S)){
		   rigidbody.velocity += camForward * -1 * speed * Time.deltaTime;
		}
		
		//Move the character left/right
		if(Input.GetKey(KeyCode.A))
			rigidbody.velocity += camRight * -1 * speed * Time.deltaTime;
		
		if(Input.GetKey(KeyCode.D))
			rigidbody.velocity += camRight * speed * Time.deltaTime;
		
		if(Input.GetKeyDown(KeyCode.Space)  grounded == true)
			rigidbody.velocity += Vector3.up * jumpHeight;
	
	}
}

Okay, so I tested your script, it works but it’s still not the thing I was looking for…

I’m basically trying to have a “FPS Controller” with a “3rd Person Camera”.

Okay, I think I can explain it better! :smile:

I want to steer the ball with the mouse, and make the ball move with the “WASD” buttons. Along with a simple line of code that will make me be able to “Jump”

Hope that helps? :slight_smile:

So you don’t want to give the player the option of rotating the camera seperately to the ball? You’d rather them be stuck with not being able to view their surroundings? or not being able to look left and travel right?

http://dl.dropbox.com/u/56346948/BallRollerWeb/BallRollerWeb.html

So just to check, it’s not controls like that which you desire? but one where if you rotate the camera while stationary, the ball will rotate to?

What you put up is exactly how I want it! :smile:

If thats the script you used, then I guess I must of done something wrong…

If you want, I can give you my build and see what I’m doing wrong?

If you want me to take a look I don’t mind at all.

PM’d you.