简体   繁体   中英

Moving a rigid body in the direction it's facing in Unity and C#

I've been trying to get this working using pretty much every method I can find (even using the dreaded transform.translate) but I just can't seem to get it working. This is just a draft of the code and if there are any other ways to go about it I'm down to change some stuff.

Currently, he barely moves (it looks like he's getting stuck on the floor somehow.) I'm fairly new to moving objects using rigid bodies so I'm pretty much in the dark on how to solve this issue.

Here's the script from my latest crack at it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerTest : MonoBehaviour
{
    public float speed = 10.0f;
    public Rigidbody rb;
    public Vector3 movement;

// Start is called before the first frame update
void Start()
{
    rb.GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown("w"))
    {
        movement = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
        }
    }
    void FixedUpdate()
    {
            moveCharacter(movement);
    }

    void moveCharacter(Vector3 direction)
    {
        rb.MovePosition(transform.position + (transform.forward * speed * Time.deltaTime));
    }
}

In your code you have your moveCharacter function inside your Update function, enclosed is the fixed one, which should work now. Before your FixedUpdate was not getting called, therefore your moveCharacter function was not as well and your GameObject was not moving.

  • EDIT 1: You shoud also multiply with your movement direction as well, updated the script to fit that
  • EDIT 2: I misplaced the curly brackets, fixed that now
  • EDIT 3: You should also update your movement Vector3 every frame, not if W is pressed, fixed the script again.
  • EDIT 4: Fixed the movement bug (copy and paste entire script, since i changed more lines)

This is the updated script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerTest : MonoBehaviour
{
    public float speed = 10.0f;
    public Rigidbody rb;
    public Vector3 movement;

    // Start is called before the first frame update
    void Start()
    {
        rb.GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        movement = new Vector3(Input.GetAxis("Horizontal"), 1f, Input.GetAxis("Vertical"));
    
    }

    void FixedUpdate()
    {
        moveCharacter(movement);
    }

    void moveCharacter(Vector3 direction)
    {
        Vector3 offset = new Vector3(movement.x * transform.position.x, movement.y * transform.position.y, movement.z * transform.position.z);
        rb.MovePosition(transform.position + (offset * speed * Time.deltaTime));
    }
}

References: Rigidbody.MovePosition

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