I want my sprite to move either up or down or left or right, I don't want any other movement such as north-west or south-east; it needs to be either up, down, right or left. There are similar posts like this on the internet but I was wondering why mine wasn't working. It doesn't initiate any movement in the game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Sprite Up;
public Sprite Down;
public Sprite Right;
public Sprite Left;
public float speed;
// Update is called once per frame
void Update()
{
Vector3 move = transform.position;
if (Input.GetKey(KeyCode.W))
{
GetComponent<SpriteRenderer>().sprite = Up;
move.z += speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
GetComponent<SpriteRenderer>().sprite = Left;
move.x -= speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
GetComponent<SpriteRenderer>().sprite = Right;
move.x += speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
GetComponent<SpriteRenderer>().sprite = Down;
move.y -= speed * Time.deltaTime;
}
transform.position = move;
}
}
For what I am looking for, I don't think I need rigidBody as most people include in their movement. I'm still a beginner so an in-depth explanation of why my code isn't working and what I could do to fix it would be helpful. Thanks.
First, you should cache your SpriteRenderer, here you're doing up to 4 GetComponent calls just for movement.
Then, you don't want to directly set the position of your object, but move it. You can either do transform.Translate(move), or transform.position += move;
edit: and you should probably assign the sprite basing on the final movement. Here you will override it every time as your ifs are not exclusive
In your case move
increases each time when if
statement pass. There are two ways to disable double sides movement: or rewrite move
in each if
or exit form function after first if
passed.
Also there is another issue in your script: you need update x
axis for A/D buttons and z
axis for W/S.
For example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Sprite Up;
public Sprite Down;
public Sprite Right;
public Sprite Left;
public float speed;
// Update is called once per frame
void Update()
{
Vector3 move;
if (Input.GetKey(KeyCode.W))
{
GetComponent<SpriteRenderer>().sprite = Up;
move = new Vector3(-speed * Time.deltaTime, 0, 0);
}
if (Input.GetKey(KeyCode.A))
{
GetComponent<SpriteRenderer>().sprite = Left;
move = new Vector3(0, 0, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
GetComponent<SpriteRenderer>().sprite = Right;
move = new Vector3(speed * Time.deltaTime, 0, 0);
}
if (Input.GetKey(KeyCode.S))
{
GetComponent<SpriteRenderer>().sprite = Down;
move = new Vector3(0, 0, -speed * Time.deltaTime);
}
transform.position += move;
}
}
Solution:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Sprite Up;
public Sprite Down;
public Sprite Right;
public Sprite Left;
public float speed;
// Update is called once per frame
void Update()
{
Vector3 move;
if (Input.GetKey(KeyCode.W))
{
GetComponent<SpriteRenderer>().sprite = Up;
move = new Vector2(0, speed * Time.deltaTime);
transform.position += move;
}
if (Input.GetKey(KeyCode.A))
{
GetComponent<SpriteRenderer>().sprite = Left;
move = new Vector2(speed * Time.deltaTime, 0);
transform.position -= move;
}
if (Input.GetKey(KeyCode.D))
{
GetComponent<SpriteRenderer>().sprite = Right;
move = new Vector2(speed * Time.deltaTime, 0);
transform.position += move;
}
if (Input.GetKey(KeyCode.S))
{
GetComponent<SpriteRenderer>().sprite = Down;
move = new Vector2(0,speed * Time.deltaTime);
transform.position -= move;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.