I need some help here. I am probably missing something but this script returns "Cannot convert UnityEngine.Component to CharacterMotorMovement. I am trying to access Unity’s character controller so that I can implement sprinting to it. Here’s my code:
using UnityEngine;
using System.Collections;
public class PlayerSprint : MonoBehaviour
{
public int sprintModifier;
private CharacterMotorMovement movement;
void Awake()
{
movement = (CharacterMotorMovement)gameObject.GetComponent("CharacterMotorMovement");
}
void Update()
{
while(Input.GetKeyDown(KeyCode.LeftShift))
{
movement.maxForwardSpeed = movement.maxForwardSpeed + sprintModifier;
}
movement.maxForwardSpeed = movement.maxForwardSpeed - sprintModifier;
}
}
movement = (CharacterMotorMovement)gameObject.GetComponent<CharacterMotorMovement>();
or
movement = gameObject.GetComponent<CharacterMotorMovement>() as CharacterMotorMovement;
Same error in both cases: The type CharacterMotorMovement' must be convertible to UnityEngine.Component’ in order to use it as parameter T' in the generic type or method UnityEngine.GameObject.GetComponent()’
I am officially stumped. LOL About the while loop, would it work correctly with an if statement?