- Home /
What is wrong about this script / why cant the player walk up and down?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour { public float moveSpeed = 5f; public Transform movePoint; public LayerMask WhatStopsMovement;
// Start is called before the first frame update
void Start()
{
movePoint.parent = null;
}
// Update is called once per frame
void Update()
{
{
transform.position = Vector3.MoveTowards(transform.position, movePoint.position, moveSpeed * Time.deltaTime);
if (Vector3.Distance(transform.position, movePoint.position) <= .0005f)
{
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) == 1f)
{
if(!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f), .2f, WhatStopsMovement))
{
movePoint.position += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f);
}
}
if (Mathf.Abs(Input.GetAxisRaw("Vertical")) == 1f)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Vertical"), 0f, 0f), .2f, WhatStopsMovement))
{
movePoint.position += new Vector3(Input.GetAxisRaw("Vertical"), 0f, 0f);
}
}
}
}
}
}
Answer by metalted · Mar 16, 2020 at 04:18 PM
movePoint.position += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f); VS movePoint.position += new Vector3(Input.GetAxisRaw("Vertical"), 0f, 0f); As you might notice, you are setting the x value of the vector3 on both cases. So you move in the x direction when using up/down and when using left/right. If you want to move forward on the z-axis, you need to assign the vertical input to the z-component of the vector3 and not the x component.
Thanks a lot! But Im building a RPG game (camera above the Player). So how should I change the script?
It all depends on what your game looks like and what axis you are playing on, one thing is certain: Assigning both axis to the same component wont work and will control the same movement. You need to find out how your player needs to move on the worlds axis. A standard top down 3D game, will be forward :Z, sideways:X, jumping/updown:Y. This is also build in to Unity with for instance, Vector3.right, Vector3.up, Vector3.forward, which are the X,Y and Z world axis respectively.
I think you just need to change the Vector3 on line 28 to: new Vector3(0,0,Input.GetAxisRaw("Vertical"));
I fixed it myself! But with your help, ins$$anonymous$$d of: movePoint.position += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f); I made: movePoint.position += new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f);
Your answer
![](https://koobas.hobune.stream/wayback/20220612225057im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Whether to use a Trigger or not. 1 Answer
OnTriggerEnter2D Running twice when destroying game object. 1 Answer
Instantiated objects do not work with Hitboxes(2D) 0 Answers
how can i make game like this one with unity (2d,2.5d) 1 Answer
Draw Gizmos for Non-Monobehaviour objects in an Array 0 Answers