Home > Back-end >  how can i convert this unity movement script to be with rigidbody?
how can i convert this unity movement script to be with rigidbody?

Time:11-25

hi everyone so basically I was wondering how I could change this movement script to be with rigidbody. It works the way I want it to with wasd to change look direction and then space to move forward from that direction kinda but I want to convert it all to be done with rigidbody so I can do collisions. Thanks!!

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

public class Fly : MonoBehaviour
{

public float moveSpeed;
void Start()
{
    
}


void Update()
{
    if (Input.GetKey(KeyCode.Space))
    {
        transform.Translate(new Vector3(0,0,moveSpeed) * Time.deltaTime, Space.Self);
    }
    

    float rotatex = Input.GetAxis("Vertical");
    float rotatey = Input.GetAxis("Horizontal");
    
    transform.Rotate(0, rotatey, 0, Space.World);
    transform.Rotate(-rotatex * 0.5f, 0, 0);
}

}

CodePudding user response:

There are quite a few errors with your code as written. No problem, but you'll probably need to fix all of these before your code starts working as intended. This answer is meant to give you a springboard to google up on some of these errors and wrap your head around the Update() method and how it works in Unity.

First, you'll need to ensure the RigidBody2d component is on your Game Object. That's easy enough to do, right click in the inspector and add the component. Google: "How do I add components to game objects in Unity"

Next, you're going to want to understand how often Update() is called and how it works. Spoiler alert - it's called as fast as your computer can. What that means is that changing the rotation with transform.Rotate every call is likely going to lead to an object that's spinning extremely rapidly. Perhaps rapidly enough that it's spinning more than 360 degrees each frame and just appears to be "twitching" randomly. You'll need to scale your rotation, so Google up "delta time in Unity Update".

Lastly, you're going to want to collect your inputs, which you're doing correctly with Input.GetKey(), although there are lots of ways to skin this cat. Perhaps the most straightforward would be to collect your inputs in one method called GetAllInputs(), set some booleans in that method to things like IsMovingRight or IsJumping and then operate on them in your Update method. This is only one approach, there are literally dozens, and many ways that they can be improved. Google up "Unity Input System", "Unity 2D Physics", and "Finite State Machines" (since you're eventually going to want to have "states" like crouching, jumping, running, hanging on to a wall, etc). It seems like you're writing a platformer, so I'd also watch some of the youtube tutorials on that, and google phrases like "Creating a Unity Platformer Tutorial".

CodePudding user response:

Assuming it works the way you want here is the code using rigidbody (Make Sure Rigid Body is attached to object) This code must be written in Fixed Update as RigidBody based calculation must be done in Fixed Update:

        public class Fly : MonoBehaviour
        {
            public float moveSpeed;
            [SerializeField] private Rigidbody rb;

            void Start()
            {
                rb = GetComponent<Rigidbody>();

            }


            void FixedUpdate()
            {
              if (Input.GetKey(KeyCode.Space))
              {
                  //transform.Translate(new Vector3(0,0,moveSpeed) * 
                  //Time.deltaTime, Space.Self);
                  if (rb)
                  {
                      rb.MovePosition(new Vector3(0,0,moveSpeed) * 
                      Time.deltaTime);
                  }
                  
              }
              

              float rotatex = Input.GetAxis("Vertical");
              float rotatey = Input.GetAxis("Horizontal");
              
              // transform.Rotate(0, rotatey, 0, Space.World);
              // transform.Rotate(-rotatex * 0.5f, 0, 0);
              if (rb)
              {
                  rb.MoveRotation(Quaternion.Euler(rotatex , rotatey , 0));
              }

            }
        }
  • Related