以下のコードを使用すると、キャラクターを移動してカメラビューに制限できます。しかし、私は入力を使用しているのでInput.GetAxisRaw ("Horizontal");
、私のキャラクターは広告キーと矢印キーの両方で動きます。しかし、私は別のキャラクターの矢印キーが欲しいです。AddForce
特定のキーを使用するifステートメントを試してみました(これは文字の移動には適していません)。しかし、コードを使用して境界を確認することはできませんでした。何か提案できますか?(ところで、私のキャラクターは水平方向にしか動きません)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBlueController : MonoBehaviour {
public float speed;
public Rigidbody2D player1rb;
public float viewpointfirst;
public float viewpointsecond;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
float horizontal = Input.GetAxisRaw ("Horizontal");
if((horizontal > 0) || (horizontal < 0))
{
Vector3 tempVect = new Vector3 (horizontal, 0, 0);
tempVect = tempVect.normalized * speed * Time.deltaTime;
Vector3 newPos = player1rb.transform.position + tempVect;
checkBoundary (newPos);
}
}
void checkBoundary(Vector3 newPos)
{
Vector3 camViewPoint = Camera.main.WorldToViewportPoint (newPos);
camViewPoint.x = Mathf.Clamp (camViewPoint.x, viewpointfirst, viewpointsecond);
Vector3 finalPos = Camera.main.ViewportToWorldPoint (camViewPoint);
player1rb.MovePosition (finalPos);
}
}
の代わりに使用するように私の古い答えを変更しました。あなたがしなければならなかったすべてはから水平方向の変数を交換したと、左に行くとき右に行くとするときに、すべて押されていないとき。残りのコードは同じままです。プレーヤー1とプレーヤー2が同じコードを必要とする異なるキーを使用するようにしたいので、同じコードを何度も書かないようにするための関数をさらに追加しました。Input.GetKey
Input.GetAxisRaw
Vector3 (horizontal, 0, 0);
-1
1
0
プレーヤー1の場合はWASD、プレーヤー2の場合は矢印キー。これらは、呼び出されたUpdate
関数で必要なキーに変更できますmovePlayer
。
public float speed = 50;
public Rigidbody2D rb1;
public Rigidbody2D rb2;
public void Update()
{
//Player one (WASD)
movePlayer(rb1, KeyCode.A, KeyCode.D, KeyCode.W, KeyCode.S);
//Player two (Arrow keys)
movePlayer(rb2, KeyCode.LeftArrow, KeyCode.RightArrow, KeyCode.UpArrow, KeyCode.DownArrow);
}
void movePlayer(Rigidbody2D targetRg, KeyCode left, KeyCode right, KeyCode up, KeyCode down)
{
Vector2 hAndV = getInput(targetRg, left, right, up, down);
Vector3 tempVect = new Vector3(hAndV.x, hAndV.y, 0);
tempVect = tempVect.normalized * speed * Time.deltaTime;
Vector3 newPos = targetRg.transform.position + tempVect;
checkBoundary(targetRg, newPos);
}
Vector2 getInput(Rigidbody2D targetRg, KeyCode left, KeyCode right, KeyCode up, KeyCode down)
{
Vector2 input = Vector4.zero;
//Horizontal
if (Input.GetKey(left))
input.x = -1;
else if (Input.GetKey(right))
input.x = 1;
else
{
input.x = 0;
targetRg.velocity = Vector3.zero;
targetRg.angularVelocity = 0f;
}
//Vertical
if (Input.GetKey(up))
input.y = 1;
else if (Input.GetKey(down))
input.y = -1;
else
{
input.y = 0;
targetRg.velocity = Vector3.zero;
targetRg.angularVelocity = 0f;
}
return input;
}
void checkBoundary(Rigidbody2D targetRg, Vector3 newPos)
{
//Convert to camera view point
Vector3 camViewPoint = Camera.main.WorldToViewportPoint(newPos);
//Apply limit
camViewPoint.x = Mathf.Clamp(camViewPoint.x, 0.04f, 0.96f);
camViewPoint.y = Mathf.Clamp(camViewPoint.y, 0.07f, 0.93f);
//Convert to world point then apply result to the target object
Vector3 finalPos = Camera.main.ViewportToWorldPoint(camViewPoint);
targetRg.MovePosition(finalPos);
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加