Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ben 39 · May 29, 2011 at 05:48 PM · collisioncharacterspeedincreasemotor

Increasing speed of Character Motor on collision help

Im trying to edit the CharacterMotor script for the FirstPersonController so that when the player collides with an object the players speed is increased.

Below is the edited part of the script that isnt working...

 function OnControllerColliderHit (hit : ControllerColliderHit) {
 if (hit.normal.y > 0 && hit.normal.y > groundNormal.y && hit.moveDirection.y < 0) {
     if ((hit.point - movement.lastHitPoint).sqrMagnitude > 0.001 || lastGroundNormal == Vector3.zero)
         groundNormal = hit.normal;
     else
         groundNormal = lastGroundNormal;
     
     movingPlatform.hitPlatform = hit.collider.transform;
     movement.hitPoint = hit.point;
     movement.frameVelocity = Vector3.zero;
      
     if(hit.gameObject.tag == "Boots") {
          maxForwardSpeed = 100.0;
     }     
 }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HHameline · May 29, 2011 at 05:52 PM 0
Share

Are you getting any errors? OR is it simply not achieving what you want it too?

avatar image Ben 39 · May 29, 2011 at 06:07 PM 0
Share

its simply not working...when the player collides with the game object tagged Boots it is meant to increase player speed but its not the case...any ideas?

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · May 29, 2011 at 06:30 PM

The CharacterMotor is a very complicated script. Editing it isn't a good idea - believe me, I tried a lot, and most times had only undesired side effects. In this case, maxForwardSpeed is just the speed limit, not the character speed. I suggest you to replace the original scripts with this one:

 var speed : float = 6.0;
 var jumpSpeed : float = 8.0;
 var gravity : float = 20.0;
 
 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);
 }

It was extracted from:
http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html

It's much easier to alter than the original CharacterMotor - just add this script to your character and disable CharacterMotor.js and FPSInputController.js.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rct3fan24 · Jul 30, 2013 at 11:58 PM 0
Share

I have a problem with this script. You walk much faster going diagonally then you do when you walk straight in one direction. It's quite annoying. Is there any way to fix this?

avatar image justinl · Jul 31, 2013 at 12:07 AM 0
Share

Define your maximum velocity, and then before you call $$anonymous$$ove(), clamp the moveDirection using Vector3.Clamp$$anonymous$$agnitude. This works for me.

avatar image aldonaletto · Jul 31, 2013 at 01:45 AM 0
Share

@justini is right - add this after line 15:

 moveDirection = Vector3.Clamp$$anonymous$$agnitude(moveDirection, speed);

This will ensure a max velocity of speed in any horizontal direction without affecting the vertical speed.

avatar image
0

Answer by justinl · Sep 24, 2012 at 11:12 AM

Wow I love the script that Aldo Naletto posted. I've ported it to C# for those who are interested:

 using UnityEngine;
 using System.Collections;
 
 public class CharacterMotor : MonoBehaviourOSP {
     
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     
     Vector3 moveDirection = Vector3.zero;
     
     new void Start () {
     
     }
     
     void Update() {
         CharacterController controller = gameObject.GetComponent<CharacterController>();
         
         
         if (controller.isGrounded == true) {
             // We are grounded, so recalculate
             // move direction directly from axes
             moveDirection = new 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);
     }
     
     
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

High speed and collisions: Character Controller falls through the ground when jumping\running at a certain speed 0 Answers

Increase speed on collision? 0 Answers

I can't increase the speed of my character?,Increase the speed of my character! 1 Answer

Character Controller - Collision Detection 1 Answer

Click on an object and then edit the speed of my player? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges