- Home /
Player won't look in direction and travel at the same time?
Hi
I'm doing a top down game and would like my player to look in the direction he is going in. I have found some code that changes the players rotation which to me looks like it should move the player. I have also got some code that will move the player backwards and forwards, left and right but I can't merge the two different section of code together which makes me think there must be another way and after looking at everyoones else's questions and solutions, I am stumped.
This changes my rotation:
void Update() { if (Input.GetKeyDown (KeyCode.W)) transform.forward = new Vector3 (0f, 0f, 1f) * speed * Time.deltaTime; else if (Input.GetKeyDown(KeyCode.S)) transform.forward = new Vector3(0f, 0f, -1f); else if (Input.GetKeyDown(KeyCode.D)) transform.forward = new Vector3(1f, 0f, 0f); else if (Input.GetKeyDown(KeyCode.A)) transform.forward = new Vector3(-1f, 0f, 0f); }
and this allows me to move:
if (Input.GetKey (KeyCode.UpArrow))
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.DownArrow))
{
transform.Translate(Vector3.forward * -speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.LeftArrow))
{
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.RightArrow))
{
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
Two different pieces of code which both do jobs I would like to do. They won't work together or even be in the same script as each other.
I have tried
transform.forward = new Vector3 (0f, 0f, 1f) += transform.Translate(Vector3.forward * -speed * Time.deltaTime;
I have also tried
transform.position += person.transform.forward * Time.deltaTime * speed;
but it won't have it. I think i'm close but I am out of ideas.
Please help if you can.
Harry
PS I know the inputs are different, its not like that in my code. I back my code up as I create and find it, this is the back up which hasn't been changed to match. :)
EDIT:
Why won't this work?
private Vector3 direction; //something like this??
void Update()
{
if (Input.GetKey (KeyCode.UpArrow))
{
direction = transform.Translate(Vector3.forward * speed * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(direction);
}
}
I know this moves the player transform.Translate(Vector3.forward speed Time.deltaTime); so wouldn't I be able to use this with transform.rotation = Quaternion.LookRotation() to change the rotation.
You are mixing lots of stuff there. transform.position it's equal to (x,y,z) or Vector3 and when you do transform.Translate() you are calling a function So you are adding a function to a Vector3.
And where are you putting al this code? on function Update()?
if you put this code: private var myScale : float;
function Start(){
myScale = transform.localScale.x;
}
function Update(){
// $$anonymous$$OVE LEFT AND RIGHT
if(Input.GetAxis("Horizontal")){
var AxisValue :float = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * AxisValue *speed);
if(AxisValue > 0){
transform.localScale.x = myScale ;
} else {
transform.localScale.x = -myScale ;
}
}
// $$anonymous$$OVE UP AND DOWN
if(Input.GetAxis("Vertical")){
transform.Translate(Vector3.up * Input.GetAxis("Vertical")*speed)
}
}
Answer by podmaster · Jul 11, 2014 at 09:20 PM
EDIT: Your game is 2D or 3D , your trying to make something like zelda or pokemon?
You are mixing lots of stuff there. transform.position it's equal to (x,y,z) or Vector3 and when you do transform.Translate() you are calling a function So you are adding a function to a Vector3.
And where are you putting al this code? on function Update()?
Try with this code:
private var myScale : float;
function Start(){
myScale = transform.localScale.x;
}
function Update(){
// MOVE LEFT AND RIGHT
if(Input.GetAxis("Horizontal")){
var AxisValue :float = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * AxisValue *speed);
if(AxisValue > 0){
transform.localScale.x = myScale ;
} else {
transform.localScale.x = -myScale ;
}
}
// MOVE UP AND DOWN
if(Input.GetAxis("Vertical")){
transform.Translate(Vector3.up * Input.GetAxis("Vertical")*speed)
}
}
I don't completly understand how you are using this code but you are using always transform.forward , if it's an up and down game don't you have to use transform.up or down, etc.?
To flip your player you can do
var myScale = transform.localScale;
if (Input.GetKey (KeyCode.LeftArrow))
{
transform.localScale = -myScale; // FLIP to the opposite direction
}
if (Input.GetKey (KeyCode.RightArrow))
{
transform.localScale = myScale; // FLIP to the original direction
}
//For moving i like to use .Translate
transform.Translate(Vector3.up * Time.deltaTime * speed)
// Use Vector3.up , Vector3.forward, Vector3.right, Vector3.down , etc
Answer by VAN-D00M · Jul 11, 2014 at 10:20 PM
Hi thanks for getting back to us.
I got the logic behind your code and tried it out but it didn't really work all that well.
The two sections of code I posted both do different jobs, one can move the other can rotate but because they are so similar they won't live in the same script as each other. Like with your code, I was trying to put the transform.Translate code and the transform.localScale in the same if statement.
if (Input.GetKey (KeyCode.LeftArrow))
{
transform.localScale = -myScale;
transform.Translate(Vector3.up * Time.deltaTime * speed);
}
I think I'm approaching it wrong but I can't think how to make it work.
You are trying to make a game like zelda or pokemon where you move up,down,left and right?
I'm trying to create a top down shooter which I would like to take the form a twin stick shooter like Geometry Wars or something similar. $$anonymous$$y aim is to have it set up for dual virtual joysticks. At the moment the game looks more like this regarding player position. http://unity3d.com/learn/tutorials/projects/space-shooter
All my code is in Void Update() in a Player$$anonymous$$ovement script attached to the player.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Player Controller Rotation Script Help 2 Answers
Help with Character Controller 1 Answer