- Home /
transforming player position on input.get button down
Hello, game developers. May someone help me out please. I am making a 3d side scroller for pc and I want to know how to change the way my player faces by pushing the left or right arrow key or a,d keys. I have my player input settings set as Vertical for left being the negative btn and right as the postive btn. What happens is that when I Press either the left or right key the player transforms to one side. Here is my script.
#pragma strict
var jumpSound : AudioClip;
var walkSound : AudioClip;
var soundPlaying : boolean = false;
var playerPosition : Transform;
function Start () {
}
function Update () {
if (Input.GetButtonDown ("Vertical")&& soundPlaying == false){
audio.PlayOneShot (walkSound);
soundPlaying = true;
playerPosition.transform.rotation.x += 180;
playerPosition.transform.rotation.y = 0;
playerPosition.transform.rotation.z += 0;
}
if (Input.GetButtonDown ("Jump")){
audio.PlayOneShot (jumpSound);
}
if (Input.GetButtonUp ("Vertical")){
audio.Stop ();
soundPlaying = false;
}
}
@script RequireComponent (AudioSource)
Well "Vertical"
is for up and down movement so ins$$anonymous$$d of confusing yourself and everyone else try using "Horizontal"
.
Here is a slightly modified code for you that rotates the player around.
But it only works once.And honestly I have no idea why, so you will have to figure out yourself how to make it work all the time.Or maybe someone here could point out how to do it and show me where I made a mistake :D
function Update ()
{
if (Input.GetButton ("Horizontal"))
{
playerPosition.transform.rotation.x += 0 * Time.deltaTime;
playerPosition.transform.rotation.y += 180 * Time.deltaTime;
playerPosition.transform.rotation.z += 0 * Time.deltaTime;
}
}
Answer by ArkaneX · Sep 02, 2013 at 08:22 PM
Please take a look at Input.GetAxis. Reading axis value will tell you which key was pressed. For "Vertical" axis, value greater than 0 is up, and value lower than 0 is down. For "Horizontal" axis positive value is right and negative is left.
Depending on the value returned, you need to either rotate left or right, but don't do this using x,y,z properties of rotation (Quaternion) - use Transform.Rotate method instead. For example, to rotate around Y axis:
if (Input.GetButtonDown("Horizontal"))
{
if (Input.GetAxis("Horizontal") > 0)
{
playerPosition.transform.Rotate(0, 90, 0);
Debug.Log("right");
}
else
{
playerPosition.transform.Rotate(0, -90, 0);
Debug.Log("left");
}
}
If required, remember to rotate back after key is released.
Don't forget to mark it as answered otherwise it's always open and people will keep looking at this post.
Your answer
Follow this Question
Related Questions
Problems with the movement of the player 1 Answer
How to detect two keys released simultaneously [Input System] 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
make player move in direction it's facing 2 Answers
Use GameObject's global position instead of local position 3 Answers