Player flies PROBLEM

Hi!

I have a script for a rolling ball player. The problem I have is when I try to go backwards the ball flies up in the air. That’s because in my script I have that the movement is changed where the camera is looking. Like if I’m holding the “W” key and spin around the camera the player should go where the camera is facing.

The script:

var ballSpeed : float;
var cameraObject : GameObject;

function Update () 
{ 
//Forward
	var vertical : float = Input.GetAxis ("Vertical");
		rigidbody.AddForce(cameraObject.transform.forward * ballSpeed * vertical * Time.deltaTime);
//Left, right, back
	var horizontal : float = Input.GetAxis ("Horizontal");
		rigidbody.AddForce(cameraObject.transform.right * ballSpeed * horizontal * Time.deltaTime);
}

Does anyone know how to fix this problem?

The length of this answer may look discouraging, but the majority of it is code to be copied and pasted.

I don’t know why your script does that, but I do have an alternative solution. Use my first (java) script, apply it to the character. Then use my two camera java-scripts on the camera. This will allow your character to move with the WASD combination, and give the camera a smooth following of your character.

Here’s the code:

//Movement.js

var speed : float = 6.0;
var twiceSpeed : float = 10.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var rotateSpeed : float = 3.0;

private var moveDirection : Vector3 = Vector3.zero;

function Update() {
	var controller : CharacterController = GetComponent(CharacterController);
	if (controller.isGrounded) {
		//Grounded, so recalculate
		//Move directly from axes
		moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
		
		//Rotation Code
		transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
		
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
		
		//Animation Code
		if (Input.GetKeyDown("w"))
		{
			animation.Play("Full Walk");
		}
		else if (Input.GetKeyUp("w"))
		{
			animation.Stop();
			animation.Play("Idle");
		}
	}
	
	//Apply Gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	//Move Controller
	controller.Move(moveDirection * Time.deltaTime);
}

Now, the camera scripts:

//Smooth Follow.js

/*
This camera smoothes out rotation around the y-axis and height.
Horizontal Distance to the target is always fixed.

There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.

For every of those smoothed values we calculate the wanted value and the current value.
Then we smooth it using the Lerp function.
Then we apply the smoothed values to the transform's position.
*/

// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we 
var heightDamping = 2.0;
var rotationDamping = 3.0;

// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")


function LateUpdate () {
	// Early out if we don't have a target
	if (!target)
		return;
	
	// Calculate the current rotation angles
	var wantedRotationAngle = target.eulerAngles.y;
	var wantedHeight = target.position.y + height;
		
	var currentRotationAngle = transform.eulerAngles.y;
	var currentHeight = transform.position.y;
	
	// Damp the rotation around the y-axis
	currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

	// Damp the height
	currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

	// Convert the angle into a rotation
	var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
	
	// Set the position of the camera on the x-z plane to:
	// distance meters behind the target
	transform.position = target.position;
	transform.position -= currentRotation * Vector3.forward * distance;

	// Set the height of the camera
	transform.position.y = currentHeight;
	
	// Always look at the target
	transform.LookAt (target);
}

And the other camera script:

//CameraRotation.js

#pragma strict

function Update () {

if (Input.GetKey(KeyCode.UpArrow)) transform.Translate(0, 0, -1);

if (Input.GetKey(KeyCode.DownArrow)) transform.Translate(0, 0, 1);

if (Input.GetKey(KeyCode.RightArrow)) transform.Rotate(0, 1, 0);

if (Input.GetKey(KeyCode.LeftArrow)) transform.Rotate(0, -1, 0);

}

I hope that this helps you.

Also, check your colliders. That has caused me to have objects flying through the fabric of Unity Time and Space many times before now.