Unity - Moving Camera and Sprint Boost Scripting help

Hello, I have created this simple ball game where you have to complete puzzles and stuff. By that, maybe moving a cube or something? I already got a Script for MOVING, PUSHING, DEATH.

CAMERA PROBLEM:

I would like to get some help on some scripting how to actually move the camera. As the player is a Sphere, I got SmoothFollow Script on the Camera to actually have the camera follow the player in third-person.

As I got this problem that camera is only looking Front, if the Player moves against the Camera, the camera doesnt swich position. As know in MMO games, you can use Mouse2 to optional move the camera wherever you want, how can I create such script as well as having SmoothFollow script on my Camera?

SPRINT BOOST:

I would also like to get some help how to get a Fast Sprint Boost by clicking LeftShift. As I got a script for pushing things with Rigid Body, how can I make that the Sphere(player) get a fast boost, like if he is moving a cube then the cube will be moved a little bit more cause the Player used the Boost. Also, an Cooldown so its balanced(in Secs)

I would be much glad if you could provide a script for that(Java or whatever) thanks! :slight_smile:

Regards,
ArminP.

For the cameraproblem I would suggest that you measure the speed of the ball in relatino to the camear position. If it is positive you do nothing, if it is negative you flip the positions. I am not familiar with the smoothfollow-camera script so I cant tell you the specifics but that is my thoughts.

Sprintbutton: If it is an instance boost you want. like a thrust you could use rigidbody.addexplosionforce:

then start a cooldown timer which you in the update function decrease by Time.deltaTime each update. When it is below 0 you should be allowed to press left-shift again.

Thanks for the Sprint Script, but it wasnt that I were looking for. But anyways thanks. Let me provide more info as I didnt inform it very well.

I just could use some to get a Script for Sprinting on “LEFT SHIFT” or whatever button. It will give the Sphere(player) a small boost so he can move some radius forward or back faster, but if you use this Sprint Boost and hit a Rigid Body Cube it will increase the power of pushing it.

And also, Cooldown as it doesnt need to be used every 2 sec.

Is it the rotation you want to be increased then? Also for the collission you can setup a manua collission. So you flag a player to be boosted and then transfer that to the collided object giving it a push(can be interpolated over time so the push decreased of time).

hmm…

I just need a quick boost when I click Left Shift, like so the ball just moves faster and stops again, just like in Metroid Prime Game. But I also want a Cooldown so it cant be abused/spammed by the Player.

Perhaps if you show us your script for moving the ball. My idea with the added force should work. It would give a short burst and nothing more but it of cause depends on your implementation of the movement.

/// This script moves the character controller forward
/// and sideways based on the arrow keys.
/// It also jumps when pressing space.
/// Make sure to attach a character controller to the same game object.
/// It is recommended that you make only one call to Move or SimpleMove per frame.

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var dead = false;

function OnControllerColliderHit(hit:ControllerColliderHit)
{
if(hit.gameObject.tag == “fallout”)
{
dead = true;
}
}

private var moveDirection : Vector3 = Vector3.zero;

function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis(“Horizontal”), 0,
Input.GetAxis(“Vertical”));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;

if (Input.GetButton (“Jump”)) {
moveDirection.y = jumpSpeed;
}
}

// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}

function LateUpdate()
{
if(dead)
{
transform.position = Vector3(38,4,11);
gameObject.Find(“Main Camera”).transform.position = Vector3(38,4,-11);
dead = false;
}
}


I have edited this script a little bit, its basically a tweaked version from the Default Moving/Jump script posted on Unity-Help Site. I just added the “DEATH” code.

A very simple boost-functionality(untested)

/// This script moves the character controller forward

/// and sideways based on the arrow keys.

/// It also jumps when pressing space.

/// Make sure to attach a character controller to the same game object.

/// It is recommended that you make only one call to Move or SimpleMove per frame.



var speed : float = 6.0;

var jumpSpeed : float = 8.0;

var gravity : float = 20.0;

var boostTimer : float = -0.01f;

var boostExtra : float = 6.0f;

private var dead = false;



function OnControllerColliderHit(hit:ControllerColliderHit)

{

if(hit.gameObject.tag == "fallout")

{

dead = true;

}

}



private var moveDirection : Vector3 = Vector3.zero;



function Update() {

var controller : CharacterController = GetComponent(CharacterController);

if (controller.isGrounded) {

// We are grounded, so recalculate

// move direction directly from axes

moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,

Input.GetAxis("Vertical"));

moveDirection = transform.TransformDirection(moveDirection);

if(boostTime>0.0f)

{

	// This will give you a static boost. You might want to increase teh boostExtra

	// from giving 0 extra in teh beginning but after 1 second give full speedbost, depends on your

	//Implementation.

	moveDirection *= (speed + boostExtra);

}

else

{

	moveDirection *= speed;

}



if (Input.GetButton ("Jump")) {

moveDirection.y = jumpSpeed;

}

//Call it whatever you want here

if(Input.GetButton("LeftShit")){

boostTimer = 3.0f;// The time(in seconds) you want the boost applied for

}

}



// Apply gravity

moveDirection.y -= gravity * Time.deltaTime;



// Move the controller

controller.Move(moveDirection * Time.deltaTime);

}



function LateUpdate()

{

if(dead)

{

transform.position = Vector3(38,4,11);

gameObject.Find("Main Camera").transform.position = Vector3(38,4,-11);

dead = false;

}

}

It seems to work, but I if I click “Left Shift” as I tweaked code for the button, it seems that the Timer doesnt go lower than 3 Secs. If I click Left Shift I am always boosting… It never stops, and it also says “BoostTimer: -1” wich I added for testing, and it seems to go to “3” and then stay there when I’m boosting.

It does work with the boost, now we just need fix this timer. :slight_smile:

omg I missed a very important line :smile:, let me fix :smile:

Fixed the boost-timer as well as added a cooldown-timer.

/// This script moves the character controller forward

/// and sideways based on the arrow keys.

/// It also jumps when pressing space.

/// Make sure to attach a character controller to the same game object.

/// It is recommended that you make only one call to Move or SimpleMove per frame.



var speed : float = 6.0;

var jumpSpeed : float = 8.0;

var gravity : float = 20.0;

var boostTimer : float = -0.01f;

var boostCooldown : float = -0.01f;

var boostExtra : float = 6.0f;

private var dead = false;



function OnControllerColliderHit(hit:ControllerColliderHit)

{

if(hit.gameObject.tag == "fallout")

{

dead = true;

}

}



private var moveDirection : Vector3 = Vector3.zero;



function Update() {

var controller : CharacterController = GetComponent(CharacterController);

if (controller.isGrounded) {

// We are grounded, so recalculate

// move direction directly from axes

moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,

Input.GetAxis("Vertical"));

moveDirection = transform.TransformDirection(moveDirection);

if(boostTime>0.0f)

{

	// This will give you a static boost. You might want to increase teh boostExtra

	// from giving 0 extra in teh beginning but after 1 second give full speedbost, depends on your

	//Implementation.

	moveDirection *= (speed + boostExtra);

	boostTimer -= Time.deltaTime;

}

else

{

	moveDirection *= speed;

	if(boostCooldown>0.0f)

		boostCooldown -= Time.deltaTime;

}



if (Input.GetButton ("Jump")) {

moveDirection.y = jumpSpeed;

}

//Call it whatever you want here

if(Input.GetButton("LeftShit")  (boostCooldown<0.0f)){

boostTimer = 3.0f;// The time(in seconds) you want the boost applied for

boostCooldown = 5.0f; // Seconds to delay another boost, AFTER the first boost is completed

}

}



// Apply gravity

moveDirection.y -= gravity * Time.deltaTime;



// Move the controller

controller.Move(moveDirection * Time.deltaTime);

}



function LateUpdate()

{

if(dead)

{

transform.position = Vector3(38,4,11);

gameObject.Find("Main Camera").transform.position = Vector3(38,4,-11);

dead = false;

}

}

Thank you very much! :slight_smile:

If you could help with one more thing… I have this “PUSH RIGIDBODY THINGS” so I can move a cube to a destination and then clear a puzzle(for example)

How can I make the BOOST script to increase the power of the push?

Here is the Push Rigidbody Things Script:


// this script pushes all rigidbodies that the character touches

var pushPower : float = 2.0;
function OnControllerColliderHit (hit : ControllerColliderHit) {
var body : Rigidbody = hit.collider.attachedRigidbody;
// no rigidbody
if (body == null || body.isKinematic)
return;

// We dont want to push objects below us
if (hit.moveDirection.y < -0.3)
return;

// Calculate push direction from move direction,
// we only push objects to the sides never up and down
var pushDir : Vector3 = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);

// If you know how fast your character is trying to move,
// then you can also multiply the push velocity by that.

// Apply the push
body.velocity = pushDir * pushPower;
}

I never use javascript so this is I am afraid beyound me. What you want to do is actually get the script of the object that has pushed you.

you want get the script and reference a bool inside it that asks if you were boosting. This is only the case if you dont care how fast you were boosting. From what I can see it actually now already works. Because an object being hit gets the hit.moveDirection, and the movedirection is based on both speed+boost.

The line that does this:
moveDirection *= (speed + boostExtra);

affects movedirection so you should be good to go :smile:.

Alright then thanks for the Boost Script! It works very well, just need to balance some things out and it’s ready to go! :slight_smile:

Now the only thing I need now is the Camera Script…

if I were you I would post another thread about it. Also post some parts of the code and if you can images to display what you want.

Alright :slight_smile:

Todilo, I think I have another problem…

The script allows to boost and everything and its perfect! But for balancing issues, you can jump with the Boost and then jump randomly to locations without losing the speed, how can I make it so the Player cant jump when he has pressed the required button for the Boost?

Regards,
Armin.

Change:
if (Input.GetButton (“Jump”)) {

to

if (Input.GetButton (“Jump”) (boostCooldown<0.0f)){

This will prevent you from jumping when you are boosting, so the boost has to fully regenerate for it to enable jump

Or if you want to allow just as soon as the boosting no longer i occuring:

if (Input.GetButton (“Jump”) (boostTime<0.0f)){

Thank you very much! :slight_smile:

Todilo, I know I’m being irratating and I’m sorry…

When the Game starts, the player can’t jump untill Sprint is used at LEAST ones. :frowning:

I used “if (Input.GetButton (“Jump”) (boostTime<0.0f)){”

Regards.