- Home /
Having trouble with a basic movement script.
Could someone explain to me what i'm doing wrong here? I'm probably using something incorrectly. I'd like to know how to properly do this.
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public int playerCanMove = 1;
void Start () {
while(playerCanMove >= 1)
{
if(Input.GetKeyDown(KeyCode.W))
{
transform.Translate(0.0f, 1.0f, 0.0f);
playerCanMove--;
}
if(Input.GetKeyDown(KeyCode.A))
{
transform.Translate(-1.0f, 0.0f, 0.0f);
playerCanMove--;
}
if(Input.GetKeyDown(KeyCode.D))
{
transform.Translate(1.0f, 0.0f, 0.0f);
playerCanMove--;
}
if(Input.GetKeyDown(KeyCode.S))
{
transform.Translate(0.0f, -1.0f, 0.0f);
playerCanMove--;
}
}
}
}
I could probably use a for loop if I really wanted as well.
Answer by deathbane · Feb 18, 2014 at 03:34 AM
Try this edited version of your code
public class PlayerMovement : MonoBehaviour {
//added float for speed;
public float speed;
//changed from Start to Update
void Update () {
//got rid of while loop
if(Input.GetKeyDown(KeyCode.W))
{
//using speed instead of a constant to make changing and reading easier
transform.Translate(0.0f, speed, 0.0f);
}
//made if statement into if else statement
else if(Input.GetKeyDown(KeyCode.A))
{
transform.Translate(-speed, 0.0f, 0.0f);
}
else if(Input.GetKeyDown(KeyCode.D))
{
transform.Translate(speed, 0.0f, 0.0f);
}
else if(Input.GetKeyDown(KeyCode.S))
{
transform.Translate(0.0f, -speed, 0.0f);
}
}
}
Note: this will not create smooth constant motion but rather whenever you press W,A,S, or D will cause the object to jerk in the corresponding direction.
Answer by blackcoper · Feb 18, 2014 at 03:05 AM
public class PlayerMovement : MonoBehaviour {
float speed = 1f;
void Update () {
if(Input.GetKeyDown(KeyCode.W))
{
transform.Translate(0.0f, speed * Time.deltaTime, 0.0f);
}
if(Input.GetKeyDown(KeyCode.A))
{
transform.Translate(speed * Time.deltaTime *-1, 0.0f, 0.0f);
}
if(Input.GetKeyDown(KeyCode.D))
{
transform.Translate(speed * Time.deltaTime, 0.0f, 0.0f);
}
if(Input.GetKeyDown(KeyCode.S))
{
transform.Translate(0.0f, speed * Time.deltaTime *-1, 0.0f);
}
}
}
This works with the player prefab if i drop it into the scene, but doesn't work when i use this script to instantiate the player. using UnityEngine; using System.Collections; public class PlayerSpawner : $$anonymous$$onoBehaviour { public Transform player; void Start () { Instantiate(player, new Vector3(Random.Range(1,100),Random.Range(1,100),-1), Quaternion.identity); } }
is it because the instantiated version is player(clone)?
check your player prefab, that should have Player$$anonymous$$ovement script before instantiated.
I was thinking it could be that too so I remade the prefab completely to check.
Your answer
Follow this Question
Related Questions
Player movement boudaries in 2D 1 Answer
Player won't stop moving while blocking 1 Answer
How can i smooth out the rotation? 1 Answer
My player doesn't turn based on the mouse when it's supposed to 0 Answers
Non slippery movement 1 Answer