Quantcast
Channel: Questions in topic: "power up"
Viewing all articles
Browse latest Browse all 49

Change speed of character using gameobject.active at runtime?

$
0
0
I have been trying to figure this out for quite some time including 2 more questions on the forums with no answers. So I'm going to go into detail on this and hopefully someone might be able to point me in the right direciton. Here is my situation: I have a parent with 2 childed objects, which I will refer to as player1 and player2. The parent has a character controller attached to it. The game starts off with player1 active and player2 inactive. When the character controller (parent) collects a powerup and enters a trigger, player1 becomes inactive and player2 becomes active. When player 2 becomes active I want the player's speed to increase. I have the power up script attached to the Power Up game object. I am also including the movement script attached to the Parent. Please note this is not all of my script... but it should be enough to understand my problem. **Power Up Script:** private var player1 : GameObject; private var player2: GameObject; private var powerup: GameObject; function Awake () { player1 = GameObject.FindWithTag("Player"); player2 = GameObject.FindWithTag("Player2"); powerup = GameObject.FindWithTag("Powerup"); player2.SetActiveRecursively(false); } function OnTriggerEnter(other : Collider){ player2.SetActiveRecursively(true); player1.SetActiveRecursively(false); powerup.SetActiveRecursively(false); } **Movement Script (attached to Parent Object):** var walkSpeed = 15; var runSpeed = 40; private var player1 : GameObject; private var player2: GameObject; function UpdateSmoothedMovementDirection () { player1 = GameObject.FindWithTag("Player"); player2 = GameObject.FindWithTag("Player2"); var h = Input.GetAxisRaw ("Horizontal"); if (!canControl) h = 0.0; movement.isMoving = Mathf.Abs (h) > 0.1; if (movement.isMoving) movement.direction = Vector3 (h, 0, 0); if (controller.isGrounded) { // Smooth the speed based on the current target direction var curSmooth = movement.speedSmoothing * Time.deltaTime; // Change Character Speed var targetSpeed = Mathf.Min (Mathf.Abs(h), 1.0); targetSpeed *= movement.walkSpeed; var newSpeed = Mathf.Min (Mathf.Abs(h), 1.0); newSpeed *= movement.runSpeed; if (player1.active) movement.speed = Mathf.Lerp (movement.speed, targetSpeed, curSmooth); if (player2.active) movement.speed = Mathf.Lerp (movement.speed, newSpeed, curSmooth); } So when I run the game there is a "NullReferenceException" on the second to last line of the player movement script: "if (player2.active). i'm assuming this because the engine assumes player 2 should be active when the game starts and he is not. What i'm trying to tell the engine to do is acknowledge when player2 becomes active that the newSpeed should be used instead of the targetSpeed. If I remove this line of code the game starts and you can control player 1 at normal speed. However once I collect the power up you cannot control the the new character because I removed the line that made it possible to control him (if (player2.active). I hope someone understands what i'm trying to do here and can help me solve this. Thank you for your time.

Viewing all articles
Browse latest Browse all 49

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>