using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float move;
public float strafe;
public float speed;
public float playerX;
public float playerY;
public float playerZ;
public Transform playerObject;
// Use this for initialization
void Start () {
playerObject = gameObject.GetComponent<Transform>();
}
// Update is called once per frame
void FixedUpdate () {
playerX = playerObject.position.x;//--Get the X,Y,Z of the player object
playerY = playerObject.position.y;
playerZ = playerObject.position.z;
move = Input.GetAxis ("Vertical");
strafe = Input.GetAxis ("Horizontal");
//--Move object
if (move == 1) {
playerObject.position = new Vector3(playerX,playerY,playerZ+1) * Time.deltaTime * speed;
}
if (move == -1) {
playerObject.position = new Vector3(playerX,playerY,playerZ-1) * Time.deltaTime * speed;
}
if (strafe == 1) {
playerObject.position = new Vector3(playerX+1,playerY,playerZ) * Time.deltaTime * speed;
}
if (strafe == -1) {
playerObject.position = new Vector3(playerX-1,playerY,playerZ) * Time.deltaTime * speed;
}
}
}
Your problem is that you are scaling the entire vector with â* Time.deltaTime * speedâ. Try something like this instead:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public float speed;
void FixedUpdate()
{
Vector3 pos = transform.position;
float move = Input.GetAxis("Vertical");
float strafe = Input.GetAxis("Horizontal");
//--Move object
if (move == 1)
{
pos.z += Time.deltaTime * speed;
}
if (move == -1)
{
pos.z -= Time.deltaTime * speed;
}
if (strafe == 1)
{
pos.x += Time.deltaTime * speed;
}
if (strafe == -1)
{
pos.x -= Time.deltaTime * speed;
}
transform.position = pos;
}
}
First of all, your script should be working. I imagine youâve forgotten to set the âspeedâ variable in the editor.
That being said, this script has a lot of problems.
Is there a reason you are requiring the axis to be at their most extreme values in order to warrant a move?
If there is, you should not be comparing float values with ==. You should be using Mathf.Approximatly()
Once again, unless there is a reason youâre comparing them at their most extreme values, this is how Iâd rewrite your script, annotated:
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
//I've set it here, but also make sure it is set in the editor
public float speed = 3f;
// None of these variables need to be kept. You can create them all locally in the method.
// Especially playerX, playerY and playerZ, as these are only float copies of the values stored in transform.position
/*
public float strafe;
public float move;
public float playerX;
public float playerY;
public float playerZ;
//You also don't need this. MonoBehaviour comes with a transform property.
public Transform playerObject;
//You also don't need this start function.
void Start () {
playerObject = gameObject.GetComponent<Transform>();
}*/
//This should be Update(), not FixedUpdate(). FixedUpdate is for making changes to physics, like adding force.
//Additionally, if it were FixedUpdate, you should be using Time.fixedDeltaTime, not Time.deltaTime
void Update () {
//Not needed
/*
playerX = playerObject.position.x;
playerY = playerObject.position.y;
playerZ = playerObject.position.z;
*/
float vertical = Input.GetAxis ("Vertical");
float horizontal = Input.GetAxis ("Horizontal");
/*
if (move == 1) {
playerObject.position = new Vector3(playerX,playerY,playerZ+1) * Time.deltaTime * speed;
}
if (move == -1) {
playerObject.position = new Vector3(playerX,playerY,playerZ-1) * Time.deltaTime * speed;
}
if (strafe == 1) {
playerObject.position = new Vector3(playerX+1,playerY,playerZ) * Time.deltaTime * speed;
}
if (strafe == -1) {
playerObject.position = new Vector3(playerX-1,playerY,playerZ) * Time.deltaTime * speed;
}*/
//Instead:
float distance = speed * Time.deltaTime;
Vector3 horizontalOffset = Vector3.right * horizontal * distance;
Vector3 verticalOffset = Vector3.up * vertical * distance;
transform.position += horizontalOffset + verticalOffset;
}
}
And here it is without the comments:
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float speed = 3f;
void Update () {
float vertical = Input.GetAxis ("Vertical");
float horizontal = Input.GetAxis ("Horizontal");
float distance = speed * Time.deltaTime;
Vector3 horizontalOffset = Vector3.right * horizontal * distance;
Vector3 verticalOffset = Vector3.up * vertical * distance;
transform.position += horizontalOffset + verticalOffset;
}
}
Edited:
Thank you both very much for your kind help. I see both of your logic and youâve helped me indeed ![]()
I have a new problem now, and its a bit weird - I think I know whats wrong but my fix didnât work, so I am stuck again. Not sure If I should create a seperate thread or just keep posting here? Anyway, I want my character to be able to Jump and land, I have keept everything to the one script only, I will post the script so you can see whats happening, leaving out only the boring movement function. Basically when I hit the space key my character jumps but when falling he just falls straight through the floor and doesnt stop as intended.
I am using a Character Controller instead of a Rigid Body or a Primative Collider - not sure if this was the right thing to do but I wanted my character to be able to walk up slopes, that ait working either (but thats another topic)
Heres the script:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
//--Movement
public float move;
public float strafe;
//--Movement Speeds
public float walkSpeed;
public float trotSpeed;
public float jogSpeed;
public float runSpeed;
public float fastRunSpeed;
public float sprintSpeed;
//--Rotate
public float modifyRotateX;//--Modify Rotate Speed
private float rotateX;
//--Jump
public float jumpAmmount;//--Value of initial jump (set to 50)
public float maxJumpHeight;
private bool playerJumping;//--Are we jumping Y/N?
private bool playerHangingInAir;
//--Gravity
public float gravity;//--Value of (set to 20)
private CharacterController controller;
void Awake() {
controller = GetComponent<CharacterController>();
}
void Update()
{
if (Input.GetButtonDown ("Jump")) {//--Get if user hits 'jump/space'
playerJumping = true;//------------If we hit space, set the playerJumping bool to true
}
}
void FixedUpdate ()
{
//-Jumping
if (playerJumping == true) {
transform.Translate (Vector3.up * jumpAmmount * Time.deltaTime);
}
if (transform.position.y >= maxJumpHeight) {
playerJumping = false;
playerHangingInAir = true;//--Get that we are at maxHeight
}
//--Gravity
if (controller.isGrounded == false && playerHangingInAir == true) {//--If we are NOT on ground and we have reached maxHeight
transform.Translate (-Vector3.up * gravity * Time.deltaTime);//----------------Enforce gravity
}
if (controller.isGrounded == true) {//--If we ARE on ground, get that we are NOT jumping and no longer hanging in air
playerJumping = false;
playerHangingInAir = false;
}
moveAndStrafe ();//--Call to Move & Strafe function
//--Rotate
rotateX = Input.GetAxis ("Mouse X");//----------------------When player moves mouse left-to-right
transform.Rotate (Vector3.up * rotateX * modifyRotateX);//--Rotate player on Y axis
}
}
I did not have a collider componant on my character. I imagined the Character Controller was a Collider because of its appearance in the editor. Silly mistake haha.
Thanks for the link fire7side
But since adding that all kinds of funky weirdness have happened! now my character flyâs and rolls around like a crazy person.