Quantcast
Channel: Questions in topic: "pick"
Viewing all articles
Browse latest Browse all 93

How To Add A Simple Speed Boost On Collision With A Specific Object With A Character Controller

$
0
0
Hi I am fairly new to unity and Im sort of teaching myself to script through trail and error. My question is, with the script I have, how do I add to the script to make it where when it hits an object it increases the speed for a certain amount of time. On my game the character is always running forward without stopping and all he needs to do is dodge some trees and rocks while he runs, but I also want to put a power up like pick up, so that when he runs into it, it increases his speed for a couple of seconds then he slows back down to his original speed. If possible add a tag to the speed boost so it knows to speed up when it collides with this specific tag. I also have it in the script that the object gets destroyed after he hits it. I am using a character controller on my guy and I already have the power up created I just need help finishing the script. This is my current script on my player. var speed: float = 4.0; // move speed var jumpSpeed: float = 8.0; // initial jump speed var turnSpeed: float = 90; // turn 90 degrees per second var gravity: float = 9.8; private var dead = false; private var cc: CharacterController; private var vSpeed: float; function Update(){ // rotates the character according to the horizontal axis (A-D): transform.Rotate(0, Input.GetAxis("Horizontal")*turnSpeed*Time.deltaTime, 0); var moveDir = transform.forward * speed; // calculate the horizontal speed if (!cc) cc = GetComponent(CharacterController); // get the CharacterController if (cc.isGrounded){ // when grounded... vSpeed = 0.0; // vSpeed is zero... if (Input.GetButtonDown("Jump")){ // unless the character jumps vSpeed = jumpSpeed; } } vSpeed -= gravity*Time.deltaTime; // apply a physically correct gravity moveDir.y = vSpeed; // add the vertical speed cc.Move(moveDir*Time.deltaTime); // and finally move the character } function OnControllerColliderHit(hit : ControllerColliderHit){ if (hit.gameObject.name == "Speed Boost"){ Destroy(GameObject.Find("Speed Boost")); } if(hit.gameObject.tag == "fallout") { dead = true; } } function LateUpdate() { if(dead) { Application.LoadLevel("Death Scene"); } }

Viewing all articles
Browse latest Browse all 93

Trending Articles