Increase Speed

I'm trying to script a cube where upon going through a bigger cube, it increases speed. Here's my code:

 using UnityEngine;
 using System.Collections;
 public class IncreaseSpeed : MonoBehaviour {
     public float speed;
     void OnTriggerEnter(Collider other) {
             Debug.Log("We've finally touched!");

The console reads the Debug Log "We've finally touched!" so it's detecting collision and the trigger does work.

So why isn't a force being added to the cube? I was thinking maybe it's because I have this in an OnTriggerEnter method and that it's only adding a force to the rigidbody for one frame?

I'd appreciate any assistance. Thank you for your time.

more ▼

asked Oct 19, 2012 at 05:43 PM

avatar image

50 63 57 67

Is isKinematic check on the game object's RigidBody?


Oct 19, 2012 at 06:29 PM Landern

It wasn't when I posted this. The object still doesn't increase speed whether I have isKinematic checked or not.

Oct 19, 2012 at 07:17 PM latsushi
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

As force is applied over time but OnTriggerEnter only happens once per event, you need to use Impulse Force Mode in your Add Force :

AddForce : http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html

ForceMode.Impulse : http://docs.unity3d.com/Documentation/ScriptReference/ForceMode.Impulse.html

all ForceModes : http://docs.unity3d.com/Documentation/ScriptReference/ForceMode.html

Replace the Addforce line in your script to :

 rigidbody.AddForce( 0, 0, speed, ForceMode.Impulse );

or you may want to use :

 rigidbody.AddForce( transform.forward * speed, ForceMode.Impulse );
more ▼

answered Oct 19, 2012 at 11:18 PM

avatar image

30.3k 171 1064 231

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Oct 19, 2012 at 05:43 PM

Seen: 1009 times

Last Updated: Nov 09, 2012 at 05:09 AM