任何方式移动我的播放器,然后控制器脚本传送

时间:2018-04-17 06:16:59

标签: c# unity3d 2d game-physics

我正试图在我的2D平台游戏中制作一个弹簧墙。然而,我试图移动我的角色的每一种方式(addforce,transform.translate甚至尝试使用弹性材料)都传送我的角色而不是移动它。我的控制器脚本不会发生这种情况。我怀疑我的脚本中的某些内容导致了这种交互,但我不确定究竟是什么。这是我的控制器脚本。任何建议将不胜感激:))

    using UnityEngine;
using System.Collections;

public class controller : MonoBehaviour
{
    //how fast he can go
    public float topSpeed = 15f;
    bool facingRight = true;
    //what direction character is facing
    bool grounded = false;
    //check if the character is grounded
    public Transform groundCheck;
    //the transform is used to see if character has touched the ground yet
    float groundRadius = 0.2f;
    //creates a ground radius for the transform circle for ground detection
    GameObject Player, Player2;
    int characterselect;
    //I'm pretty sure this stuff ^^ is unnessecary and is from when I had the character switch in this script but dont want to remove just in case
    public float jumpForce = 700f;
    //the characters jumpforce
    public GameObject jumpParticles;
    public LayerMask whatIsGround;
    //what layer is the ground
    void Start()
    {
        characterselect = 1;
        Player = GameObject.Find("Player");
        Player2 = GameObject.Find("Player2");
        //loads game objects as variables I'm pretty sure this stuff ^^^^ is unnessecary and is from when I had the character switch in this script but dont want to remove just in case

    }

    void FixedUpdate()
    {
        //has the transform hit the ground yet returns a true or false value
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);

        // get direction
        float move = Input.GetAxis("Horizontal");
        //add velocity to the move direction times by the speed
        GetComponent<Rigidbody2D>().velocity = new Vector2(move * topSpeed, GetComponent<Rigidbody2D>().velocity.y);
        if (move > 0 && !facingRight) //if facing not right then use the flip function
            flip();
        else if (move < 0 && facingRight)
            flip();
        //the whole flip turned out not to be nesseary as my sprites were symetric

    }

    void Update()
    {
        //if the character is in fact touching the ground then when space is pressed the function will run
        if(grounded&& Input.GetKeyDown(KeyCode.Space))
        {
            //adds the jump force to the rigidbody attached to the character
            GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
            //Instantiate(jumpParticles, transform.position, transform.rotation);
            //Destroy(jumpParticles, 3f);
        }
        {
            //this was code I was working on for character switching but couldn't get it to work properly. I didn't delete it because it took me ages to do and was recycled in the characterswitch script
          //  if (Input.GetKeyDown(KeyCode.E))
          //  {
            //    if (characterselect==1)
            //    {
             //       characterselect = 2;
              //  }
             //   else if (characterselect==2)
             //   {
              //      characterselect = 1;
             //   }
         //   }
          //  if (characterselect==1)
          //  {
            //    Player.SetActive(true);
              //  Player2.SetActive(false);
         //  }
           // else if (characterselect==2)
           // {
             //   Player.SetActive(false);
               // Player2.SetActive(true);
           // }
        }
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            topSpeed = topSpeed * 2;
        }
        else if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            topSpeed = 15;
        }

    }

    void flip()
    {
        //for when facing the other direction
        facingRight = ! facingRight;
        //load the local scale
        Vector3 theScale = transform.localScale;
        //flip character on the x axis
        theScale.x *= -1;
        //and then apply it to the local scale
        transform.localScale = theScale;


    }

编辑

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

public class boost : MonoBehaviour {
private Rigidbody2D rb2d;

// Use this for initialization
void OnCollisionEnter2D(Collision2D other){
    if (other.gameObject.tag == "booster") {
        Vector2 tempvect = new Vector2 (2, 0);
        rb2d.MovePosition ((Vector2)transform.position + tempvect);
    }


}
void Start () {
    rb2d = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update () {

}

}

这是我认为应该使错误发生的代码 rb2d.MovePosition((Vector2)transform.position + tempvect);

0 个答案:

没有答案