我正在制作一个平台游戏,你必须在关卡的不同方面改变角色。每个角色都有不同的属性等。我观看了本教程https://www.youtube.com/watch?v=vD5pW97-050但这基本上只是在更改精灵,因为两个角色都是具有相同刚体和对撞机的同一个游戏对象的一部分。我正在使用 c#,如果有人知道解决方案,我真的很想得到一些帮助。这是我的播放器控制器脚本,其中包括教程中的角色切换脚本。任何帮助将不胜感激谢谢!
using UnityEngine;
using System.Collections;
public class controller : MonoBehaviour
{
public float topSpeed = 15f;
bool facingRight = true;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
GameObject Player, Player2;
int characterselect;
public float jumpForce = 700f;
public LayerMask whatIsGround;
void Start()
{
characterselect = 1;
Player = GameObject.Find("Player");
Player2 = GameObject.Find("Player2");
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
float move = Input.GetAxis("Horizontal");
GetComponent<Rigidbody2D>().velocity = new Vector2(move * topSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight)
flip();
else if (move < 0 && facingRight)
flip();
}
void Update()
{
if(grounded&& Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
{
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);
}
}
}
void flip()
{
facingRight = ! facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}