Player Moving forward and backward, instead of side-to-side

Well, I’m stumped again. Here’s my code for my player that is moving

#pragma strict


public var TheCamera : GameObject;



function Start () {
	Screen.showCursor=false;
	
}

function Update () {
transform.forward=TheCamera.transform.forward;

	if(Input.GetKey(KeyCode.W)){
	transform.Translate(0,0,5*Time.deltaTime);
}else if(Input.GetKey(KeyCode.S)){
	transform.Translate(0,0,-5*Time.deltaTime);
	
}else if(Input.GetKey(KeyCode.D)){
	transform.Translate(0,5*Time.deltaTime,0);

}else if(Input.GetKey(KeyCode.A)){
	transform.Translate(0,-5*Time.deltaTime,0);

}
}

And here is my code for my camera so that it follows the player

#pragma strict
public var ThePlayer : GameObject;
function Start () {
	
}

function Update () {
	transform.position.x=ThePlayer.transform.position.x;
	transform.position.z=ThePlayer.transform.position.z;
}

As you can see here, when I press A or D on my keyboard. My Player should move horizontally, but the D key moves my character forward, and my A key moves my player backward. Another weird thing is that the player doesn’t go as fast forwards/backwards as if I were to press W or S. Please help, and asks questions if you have any. :slight_smile:

Hello bud, Gruffy again here…

Take your ifs and separate them into individual if statements.
This will see they are each checked per Update() and not based on the conditional of one or the other being true or not…which is what you have here.

Hope that helps

regarding your camera shakey stuff is probably because of it all happening in Update (imagine its trying to update the camera the same time as its trying to update the transform`s translation), take this script below and add it to the camera in the inspector(un-checking your other camera script of course)…
Drag the object you want the camera to follow into the script that you just added to camera in inspector.

play with your distance and height parameters the script offers in the inspector…
Enjoy dude.
Gruffy

(I use this script all the time, its well handy and a nice template for any expansions)

It was converted form the Unify community offering in UnityScript

using UnityEngine;
using System.Collections;
/// <summary>
/// Smooth cam follow.
/// Converted by Gruffy (Gareth Wright)2013 from the Unity Scripts "SmoothFollow.js" script
/// Rewritten in Csharp to conform to the project`s CSharp only program code rule.
/// This camera smoothes out rotation around the y-axis and height.
/// Horizontal Distance to the target is always fixed.
/// There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
/// For every of those smoothed values we calculate the wanted value and the current value.
/// Then we smooth it using the Lerp function.
/// Then we apply the smoothed values to the transform's position.
/// </summary>
public class SmoothCamFollow : MonoBehaviour 
{
/// <summary>
/// The target we are following
/// </summary>
public Transform target;
/// <summary>
/// The distance in the x-z plane to the target
/// </summary>
public float distance = 10.0f;
/// <summary>
///  the height we want the camera to be above the target
/// </summary>
public float height = 5.0f;
/// <summary>
/// The height damping.
/// </summary>
public float heightDamping = 2.0f;
	/// <summary>
	/// The rotation damping.
	/// </summary>
public float rotationDamping = 3.0f;
	/// <summary>
	/// The target point.
	/// </summary>
private Vector3 targetPoint;
	/// <summary>
	/// The target rotation.
	/// </summary>
private Quaternion targetRotation;
	/// <summary>
	/// Late update.
	/// find the target and set the rotation to turn toward
	/// do same for world height of transform
	/// aim this transforms look direction at the target transform
	/// dampen correctional positoning and rotation
	/// </summary>
	void LateUpdate () 
	{
		if (!target)
		return;	
	
	float wantedRotationAngle = target.eulerAngles.y;
	float wantedHeight = target.position.y + height;
		
	float currentRotationAngle = transform.eulerAngles.y;
	float currentHeight = transform.position.y;
	
	 
	currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); /// Damp the rotation around the y-axis

	
	currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime); /// Damp the height

	
	var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0); /// Convert the angle into a rotation
	
	
	
	transform.position = target.position; /// Set the position of the camera on the x-z plane to:
	transform.position -= currentRotation * Vector3.forward * distance; /// distance meters behind the target

	
	transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z); /// Set the height of the camera
	
	
	transform.LookAt (target); /// Always look at the target - bad use of Unity in built heavy method
	
	}
}

NOTE: this uses transform.LookAt(); method which isnt entirely optimized but does offer a simple way to rotate the camera towards your inspector specified transform.

:slight_smile: Happy new year again dudes…

Gruffy :slight_smile:

I am not a smart man. I have been thinking about this for like 30 minutes now going crazy. I put the numbers in the Y value. Well I feel stupid.