简体   繁体   English

带2D角色控制器的Addforce()unity3d

[英]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. 我正在开发2D游戏级别,但要使用Unity3d和c#编码在3D环境中进行开发。 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. 我为我的角色制作了这个控制器,因为我希望它跟随鼠标的位置(就像小偷小偷的控件一样),但是我的角色真的很疯狂,当我单击它时,它只是在移动...嗯,虽然看起来不那么随意,但周围确实有些发抖我的鼠标位置...这是我的代码,如果您有任何不错的教程或Tiny Thief游戏之类的东西,请告诉我。 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. 无需检查目标位置是否完全匹配 (这种情况极不可能发生),而是检查角色是否已在1个单位内接近它。

I think that your 1 here is too small. 我认为您这里的1太小了。 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. 如果您将该数字增大(也许可以尝试看看哪个值合适),我认为它将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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