Help with Character Movement Always Forward Script

Hello,

I am starting to learn script and I am having trouble with getting my script to have movement be forward always.
instead of moving forward, back, left and right I am trying to have it where if I press the arrow key in any direction the z axis will keep the character facing front always.

Forgive me if its not very clear.
I don’t need a complete answer, but a good hint into the right direction. :slight_smile:
I am creating small goals for learning at the moment :slight_smile:

This is what I have at the moment:

public class PlayerMovement : MonoBehaviour 
{
	
	
	//Variables for Movement Attributes
	public float moveSpeed = 10f;
	public float turnSpeed = 50f; 
	
	//function for imput
	void Update ()
		
	{
		
		if(Input.GetKey(KeyCode.UpArrow))
			transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
		
		if(Input.GetKey(KeyCode.DownArrow))
			transform.Translate (Vector3.back * moveSpeed * Time.deltaTime);
		
		if(Input.GetKey(KeyCode.LeftArrow))
			transform.Translate (Vector3.left * moveSpeed * Time.deltaTime);
		
		if(Input.GetKey(KeyCode.RightArrow))
			transform.Translate (Vector3.right * moveSpeed * Time.deltaTime);
                   }

}

Thanks!

Hello, i dont know the answer or anything but i know that you need to put your code into these:
[ CODE ]
Your Code here
[ /CODE ]

Remove the spaced inside of the brackets

-Jonas Rasmussen

Added! Yeah that helps with showing code for sure!

Hi GameIntern,

It sounds to me like what you really want is for the camera to always be behind the player. Then when you press forward the player will move along the access he is facing. Would that be an accurate description of what you are trying to achieve?

If you simply want to always translate on one access you can adjust each key-code if statement to multiply against that adjustment.

Yes, I want if for example I press the Left, Right, Up, Back Arrows The Character will always be facing in that direction.
Could you elaborate on what that looks like: " multiply against that adjustment "
Im thinking this:

   if(Input.GetKey(KeyCode.UpArrow     *  THE ADJUSTMENT  ))

           transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);

Thanks

I think I am getting an the idea now.

You want a character to face one direction but when left or right are pressed he steps left and right still facing the same direction?

You should not change his rotation and you apply the change to his transform it should be pretty straight forward.

If I am correct in understanding what you trying to do please post your current code and I will try give you some feedback on specifically whats wrong. (assuming I understand what you want to do)

Yes, I think that explains it. Think of it like playing A NES Zelda for example, when you Press the D-Pad Arrow Up, Link is facing up going forward however when you press the left D-Pad Arrow he now will be facing in Left direction going forward.

I tried a few pieces of code but haven’t found the right piece to work:
Note: I know this doesn’t work but not sure why other than the error message. Am I close our way off? :slight_smile:

I then tried to simply rotate the Character Object…but I am using the code wrong for sure.

Thanks!

C#

public class PlayerMovement : MonoBehaviour 
{
	
	
	//Variables for Movement Attributes
	public float moveSpeed = 10f;
	public float turnSpeed = 50f; 
	
	private int playerFront; 
	private int playerBack; 
	private int playerLeft; 
	private int playerRight;
	

	
	//function for imput
	void Update ()
		
	{
	
		 playerFront = Transform.Rotate (0,0,0)  ;
		 playerBack = Transform.Rotate (0,180,0) ; 
		 playerLeft = Transform.Rotate (0,-90,0) ;
		 playerRight = Transform.Rotate (0,90,0) ;
		
		
		if(Input.GetKey(KeyCode.UpArrow))
			playerFront = transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
		
		if(Input.GetKey(KeyCode.DownArrow))
			playerBack = transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
		
		if(Input.GetKey(KeyCode.LeftArrow))
			playerLeft = transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
		
		if(Input.GetKey(KeyCode.DownArrow))
			playerRight = transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);

Thanks for the help.

Okay, I I may have found a solution:

public class PlayerMovement : MonoBehaviour 
{
	
	
	//Variables for Movement Attributes
	public float moveSpeed = 10f;
	public float turnSpeed = 50f; 
	

	
	//function for imput
	void Update ()
		
	{
	
		
		//
		
		if(Input.GetKey(KeyCode.UpArrow)) 
			transform.eulerAngles = new Vector3(transform.eulerAngles.z * Time.deltaTime, 0f, 0f);
		
		if(Input.GetKey(KeyCode.UpArrow)) 
			transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
			
		
		//
		
		if(Input.GetKey(KeyCode.DownArrow))
			transform.eulerAngles = new Vector3(transform.eulerAngles.z * Time.deltaTime, 180f, 0f);
		
		
		if(Input.GetKey(KeyCode.DownArrow))
			transform.Translate (-Vector3.back * moveSpeed * Time.deltaTime);
		
		
		//
		
		if(Input.GetKey(KeyCode.LeftArrow))
			transform.eulerAngles = new Vector3(transform.eulerAngles.x * Time.deltaTime, -90f, 0f);
			
		if(Input.GetKey(KeyCode.LeftArrow))
			transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
		
		
		//
		
		if(Input.GetKey(KeyCode.RightArrow))
			transform.eulerAngles = new Vector3 (transform.eulerAngles.x * Time.deltaTime, 90f, 0f);
		
		if(Input.GetKey(KeyCode.RightArrow))
			transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
		
			}
	

}

I change my code a bit with a very basic understanding on using: transform.eulerAngles.
It seems to work for what I was trying to imitate however I am not sure if Using the if statements like this is good practice.

Glad you came right,

I would suggest a more structured approach to your code, but there is nothing “wrong” with it.

I generally separate the choice to go left right and up from the implementation of doing so. This means you could call go left from another piece of code and it would execute. Also means you could change what going left means and all the control code doesn’t have to change.

Good luck

if(...) ...

or you can add { } and have more than one line of code governed by the if statement. The { } add a “scope” block, scope is quite important and if you’re not familiar with it, I’d suggest reading up on it.

if(...)
{
...
...
}

So,

GetKey works with getting the face direction using Vector.Forward.

But now I am trying to get the GetAxis to do it.
This is what I have:

using UnityEngine;
using System.Collections;

public class PlayerAxisMovement : MonoBehaviour 
{
    public float speed = 10.0F;
    public float rotationSpeed = 10.0F;

	
    void Update() 
	{
        float translationV = Input.GetAxisRaw("Vertical") * speed;
        float translationH = Input.GetAxisRaw("Horizontal") * speed;
		
		
		
		// Arithemetic Operators using different style but mean the same thing
        translationV = translationV * Time.deltaTime;
        translationH *=  Time.deltaTime;
		
		
		// ( X , Y, Z ) Axis Location Reference
        transform.Translate(0, 0, translationV);
        transform.Translate(translationH, 0, 0);
		
		
		//Right
		if (Input.GetAxisRaw("Horizontal") > 0)
		{
			
			 
			
		}
		
		//Left
		else if (Input.GetAxisRaw("Horizontal") < 0)
		{
		
			
		}
		
		//Up
		else if (Input.GetAxisRaw("Vertical") < 0)
		{
			
			
			
		}
		
		//Down
		else if (Input.GetAxisRaw("Vertical") < 0)
		{
			
			
		}
		
		
    }
}

So basically I am trying to get the player face in the direction he is moving.

I Have the Player Attached to a Empty Object. (The Player Object is a Child of the Empty Object)
The Empty Object has the Movement Script.

If I could effect the Facing direction of the Player Object and not the Empty Object it may work.

If someone could teach/ help explain Affecting a Child Object.

Thanks in Advance! :slight_smile:

You could do something similar to this, although quaternions are possibly a bit advanced for where you’re at they’re also kind of necessary, I’d delve into them ASAP as it provides some really useful (or necessary) functionality.

You can use Quaternion.Lerp instead of Slerp if you want a linear rotation rather than the ease in/out. The turn speed being referenced there would be: public float turnSpeed = 5f;

Hope this helps you somewhat rather than confuses you :wink:

moveVector = new Vector3(Input.GetAxis("Horizontal"), moveVector.y, Input.GetAxis("Vertical"));

Vector3 currentRotation = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

if(currentRotation != Vector3.zero)
{
	Quaternion lookRotation = Quaternion.LookRotation(currentRotation, Vector3.up);

	transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, turnSpeed * Time.deltaTime);
}

Also if you do something like (off the top of my head, may not work this way)
player = GameObject.FindGameObjectWithTag(“Player”);

And tag your player as player (rather than the empty object) you should be able to then do something like
player.transform.position or whatever you need.

Thank you Very much for your help and hints.

I was able to get it working using Get Component and Euler angles on the Child Player Object.
So, The GetAxis Script for movement is on my Empty Object (Named: Main Player)
I then Used get Component for Player ( Actual Object I needed to change direction) which was a child of the Empty Object.
Using if Statements I change the Euler Angle of the Child. :slight_smile:

Example Coded If Statement C# :
Note: boxPlayer is a Child of The Empty Object

                                    //Right
		if (Input.GetAxisRaw("Horizontal") > 0)
		{		
		boxPlayer.eulerAngles = new Vector3(transform.eulerAngles.x * Time.deltaTime, 90f, 0f);
		}

Now…

While this is working fine,
What are the limitation or issues that come when using Get Component like this ?