- Home /
Click on an object and then edit the speed of my player?
Hello, I'am new to all the scripting languages that unity uses, so I would ask if someone maybe knew how to make a script that will do the following:
When I click on an in-game object (such as a box) (A hotkey would also work), it should change the speed of the player to something like 12 (I'am using the standard 'Character Controller').
The "property" to change the speed is: First Person Controller>Character Motor (Script)>Movement>Max Forward Speed
Thanks, Andreas :)
I know other scripting languages, so I'am able to understand most scripts :)
Answer by IgnoranceIsBliss · Nov 09, 2012 at 09:59 PM
The hard part is actually detecting the click. This is a C# example for a hotkey...
void Update()
{
//Step One - Detect The Key Being Pressed...
if (Input.GetKeyDown(KeyCode.Space))
{
//Now, get the 'Character Motor' component from our object (I'm assuming that this script is on the object you want to control)
CharacterMotor Mtr = GetComponent<CharacterMotor>();
//And change the maximum forward speed
Mtr.movement.maxForwardSpeed = 12;
}
}
The intellisense will really help you here - once you have your CharacterMotor object, just type 'Mtr' followed by a period (.) and all of the options and members of this particular object will appear.
If you are going to script in Unity, make sure you are quite comfortable with one of the main languages (my personal preference is C#).
Thanks, I'am sure that this is just what I'am looking for, but when I click "Play" in Unity3D it says:
error CS0246: The type or namespace name `Character$$anonymous$$otor' could not be found. Are you missing a using directive or an assembly reference?
I placed the script in thee "root" folder, and now it works :), Thank you so much for the script :)
If he provided the solution, you can accept the answer by clicking the gray tick to the left of the answer. That way helpful people, looking through answers, can see that you already have a solution AND if somebody in the future, with the same question, knows that this one helped you to at solution :)
Thanks, I'm still new to the forums, I'll do that from now :)
Your answer
Follow this Question
Related Questions
Check CharacterController's Velocity 0 Answers
interpolate a move variable 0 Answers
Rotate Character Controller 1 Answer
Character movement relative to both camera and transform 2 Answers
Rotate character controller on all axis 0 Answers