简体   繁体   中英

Why is my Player moving Down on Screen In Unity

I am not sure why but I am starting a new project in which a player moves with WASD and points towards the mouse pointer. This is all the code I have so far as It is only early into development but any help would be nice. Here is a little pic of what I currently have on my player...

ANY HELP WILL BE AMAZING!

using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 5f;

    public Rigidbody2D rb;

    Vector2 movement;

    // Update is called once per frame
    void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");
        Vector3 mousePosition = Input.mousePosition;
        mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
        Vector2 direction = new Vector2(
        mousePosition.x - transform.position.x,
        mousePosition.y - transform.position.y
        );
        transform.up = direction;
    }

    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    }
}

Set the gravity scale to 0 or set Rigidbody2D body type to Kinematic. Kinematic rigidbodies can only be moved via script and are not participated in regular physics unlike dynamic rigidbody. So if you don't want some 'automatic' behaviors always use kinematic body type, otherwise if only problem is automatic 'moving down' issue then set gravity scale to 0 and that's all. I think both will work in your case. And also here is some further info

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM