- Home /
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;
}
}
}
Are you getting any errors? OR is it simply not achieving what you want it too?
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?
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.
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?
Define your maximum velocity, and then before you call $$anonymous$$ove(), clamp the moveDirection using Vector3.Clamp$$anonymous$$agnitude. This works for me.
@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.
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);
}
}