简体   繁体   中英

Addforce () unity3d with 2d character conroller

I am developing a 2D game level, but making it in a 3D environment using Unity3d and c# coding. I made this controller for my character as i wanted it to follow mouse position (like tiny thief controls), but my character just got really crazy and when I click it just moves... Well not as randomly as it looks but really shaky around my mouse position... This is my code and if you have any good tutorial or something like the Tiny Thief game, please do tell me. Thank you.

using UnityEngine;
using System.Collections;


//  [RequireComponent(typeof (PlatformerCharacter2D))]
using System;


public class Robot_Moves : MonoBehaviour
{
    private Vector3 wantedPos;
    private Vector3 mousePos;
    private float relativePos;
    [HideInInspector]
    public bool
        facingRight = false;

    void Update ()
    {
        if (Time.time > 2) {
            if (Input.GetMouseButtonDown (0)) {
                mousePos = Input.mousePosition;
                mousePos.z = 100f;
                mousePos.y = 0f;
                wantedPos = Camera.main.ScreenToWorldPoint (mousePos);
                Debug.Log (wantedPos);

            }
            relativePos = wantedPos.x - transform.position.x;
            if (Mathf.Abs (relativePos) > 1) {
                transform.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (relativePos, 0f) * 40f, ForceMode2D.Force);
            }
        }
    }
    void FixedUpdate ()
    {
        if (relativePos > 0 && facingRight)
            Flip ();
        else if (relativePos < 0 && !facingRight)
            Flip ();
    }

    void Flip ()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
}

This appears to be an example of what I like to call a "slingshot problem". You tell your character to move towards a point, it overshoots that point, tries to move back toward that point, overshoots it again, and repeats that forever.

Your controls make use of a "target position". Each frame, add force to the character's rigidbody until he reaches the target position. Once he's there, stop adding force.

relativePos = wantedPos.x - transform.position.x;
if (Mathf.Abs (relativePos) > 1) {
    transform.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (relativePos, 0f) * 40f, ForceMode2D.Force);
}

this section here is a good step towards solving the slingshot problem. Instead of checking for an exact match on the target position, which is extremely unlikely to happen, you check if the character has gotten sorta close to it, within 1 unit.

I think that your 1 here is too small. It's a small window that is most likely being overshot consistently causing your character to wiggle around its destination point instead of stopping near it. If you make that number larger (maybe experiment to see what value is appropriate), I think it would work correctly.

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