当前位置:网站首页>Unity makes a small planet

Unity makes a small planet

2022-04-23 22:10:00 AlphaIcarus

The production process

Create a new sphere in the scene (Planet) And a capsule (Player), Scale and add materials appropriately , Here, the capsule will be regarded as a player

Then set the camera to capsule (Player) A child object of

Adjust the appropriate camera angle by yourself
New script GravityArrtacter, Add to Planet On

public class GravityAttracter : MonoBehaviour
{
    public float gravity = -10;	// The magnitude of gravity 
    public void Attract(GameObject obj)	// The way to attract objects , Pass in the object that needs to be attracted 
    {
        Transform body = obj.GetComponent<Transform>();
        Vector3 gravityUp = (body.position - transform.position).normalized;	// Planet to player vector 
        Vector3 bodyUp = body.up;	// The positive upward direction of the object being attracted 

        body.GetComponent<Rigidbody>().AddForce(gravityUp * gravity);// Attract objects , The magnitude of gravity is negative , The direction changes to the direction of the object to the planet 

        // Rotate an axis from parameter 1 to parameter 2 ( World space ) The direction of , Here is to rotate the top of the object to the direction of gravity ,
        // But an axis cannot determine the state of rotation of an object ,* body.rotation Get the target direction that the object needs to rotate 
        Quaternion targetRotation = Quaternion.FromToRotation(bodyUp, gravityUp) * body.rotation;
        // Interpolation , rotate 
        body.rotation = Quaternion.Slerp(body.rotation, targetRotation, 50 * Time.deltaTime);
    }
}

Set your own gravity , Negative numbers here indicate the direction of gravity

New script Body, Add to Player On

public class Body : MonoBehaviour
{
    public FauxGravityAttracter attracter;	// Declare the planet's gravity 
    private Rigidbody myRigidbody;
    
    void Start()
    {
        myRigidbody = gameObject.GetComponent<Rigidbody>();	// Get the object rigid body 
        myRigidbody.constraints = RigidbodyConstraints.FreezeRotation;// Cancels rigid body collision rotation effects 
        myRigidbody.useGravity = false;		// Cancel the influence of world space gravity 
    }

    void Update()
    {	
        attracter.Attract(gameObject);// Pass in the parameter , The object is attracted 
    }
}

Set the corresponding parameters

newly build PlayerController, Add to Player On

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 15f;
    private Vector3 moveDir;
    private Rigidbody myRigidbody;

    void Start()
    {
        myRigidbody = gameObject.GetComponent<Rigidbody>();
    }
    private void FixedUpdate()
    {
        myRigidbody.MovePosition(myRigidbody.position + transform.TransformDirection(moveDir) * moveSpeed * Time.deltaTime);
    }

    void Update()
    {
        moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
    }
}

Set the movement speed

This simulates walking on the surface of the planet

版权声明
本文为[AlphaIcarus]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/113/202204232204260286.html