- Home /
Player Controller Rotation Script Help
Hello, I'm in the early stages of a game design. It's between a third person and top down shooter. I'm not the programming type, more of a designer, so I need help making a script. At the moment, all I have is a cube and the ground. I have a script that moves the character, but not quite how I would like it. There is also no gravity. I need a script that, when pressing "w" or "s" the cube moves back and forth and when I press "a" or "d" , I want the cube to rotate rather than moving like "w" and "s" movements. I also have the camera under the player cube in the hierarchy so the camera moves with the cube
Sorry for the lack of code, but this seems to be a rather simple question that would be answered by checking out some tutorials. Anyway you should be able to get this done by using the following things:
1) http://docs.unity3d.com/Documentation/ScriptReference/Input.Get$$anonymous$$eyDown.html
2) http://docs.unity3d.com/Documentation/ScriptReference/Transform.Translate.html
3) http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html
There are other ways of doing it, but this should get your cube moving. :)
These references has helped a ton, ShadoX. And for that I am very thankful. I have gotten it working, however it only moves 1 unit/second, and I have to mash the keys for it to move more. How can I make it so I can hold the key and change how much is moved?
Sorry, forgot about that. You probably want to multiply the value with Time.deltaTime http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html
Here is my code I am using to move my cube. I have tried multiplying the Time.deltatime but I cannot seem to get it to work. It is a Javascript code as well. I tried adding a variable like the link you gave me, but does the same thing. Not sure where to go from here. Thanks ShadoX
function Update()
{
//$$anonymous$$oves Player Foward 1 unit/second
if(Input.Get$$anonymous$$eyDown("w"))
transform.Translate(Vector3.forward * Time.deltaTime);
//$$anonymous$$oves Player Back 1 unit/second
if(Input.Get$$anonymous$$eyDown("s"))
transform.Translate(Vector3.back * Time.deltaTime);
//Rotates Plater Right 1 unit/second
if(Input.Get$$anonymous$$eyDown("d"))
transform.Rotate(Vector3.up * Time.deltaTime);
//Rotates Plater Left 1 unit/second
if(Input.Get$$anonymous$$eyDown("a"))
transform.Rotate(Vector3.down * Time.deltaTime);
}
Answer by save · Sep 11, 2013 at 11:05 PM
What I always say is that you need to break apart the problem, what you basically need is input which controls movement and rotation. First of all, these are, not to anyones surprise, the most asked questions - so searching for them would serve you a fair amount of results. Unity also have a lot of sweet examples in their scripting reference, for instance - the GetAxis in the Input class shows you exactly what you want to achieve.
I would like to further refer you to the community part where you could find programmers to team up with. If you want to get deeper into scripting the scripting reference and scripting section in the manual is a great place to start.
Answer by stuntrunt · Sep 13, 2013 at 02:45 AM
Here is my revised code I have working now. However, how can I make it so that my mouse cursor does the same, or replaces the left and right arrow keys that controls rotation? This will be implemented into Android once I have a working beta up so the movements will be controlled via touch, but I'll cross that bridge when I come to it. Also, how do you add gravity without using the RigidBody, or use the RigidBody but smooth it out so it isn't so jerky.
function Update()
{
var walkSpeed: float = 10;
var rotateSpeed: float = 175;
var jumpHeight: float = 50;
var runSpeedMultiplyer: float = 1.5;
//Moves Player Foward
if(Input.GetKey("w"))
transform.Translate(Vector3.forward * Time.deltaTime * walkSpeed);
//Moves Player Back
if(Input.GetKey("s"))
transform.Translate(Vector3.back * Time.deltaTime * walkSpeed);
//Moves Player Right
if(Input.GetKey("d"))
transform.Translate(Vector3.right * Time.deltaTime * walkSpeed);
//Moves Player Left
if(Input.GetKey("a"))
transform.Translate(Vector3.left * Time.deltaTime * walkSpeed);
//Rotates Player Right
if(Input.GetKey("right"))
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed);
//Rotates Plater Left
if(Input.GetKey("left"))
transform.Rotate(Vector3.down * Time.deltaTime * rotateSpeed);
//Player Jumps
if(Input.GetKeyDown("space"))
transform.Translate(Vector3.up * Time.deltaTime * jumpHeight);
//Player Runs
if(Input.GetKey("up"))
transform.Translate(Vector3.forward * Time.deltaTime * walkSpeed * runSpeedMultiplyer);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Joystick for WASD & Joystick for Camera? 0 Answers
BCE0044: Expecting : Found = Error! 2 Answers
Help with fps script 2 Answers