仿“息壤” 的第三人称 移动控制
左手摇杆?操作移动和旋转? ?右手控制摄像机绕自己旋转? ? ?先上效果?再贴代码
?
JoyBtn.cs? 控制?摇杆?进行移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JoyBtn : JoyStickButton
{
public RectTransform Background;
public RectTransform Knob;
[Header("Input Values")]
public float Horizontal = 0;
public float Vertical = 0;
public float offset;
Vector2 PointPosition;
// Use this for initialization
public override void onTouchBegan(Touch pos)
{
Debug.Log(pos.position);
}
public override void onTouchEnd(Touch pos)
{
PointPosition = new Vector2(0f, 0f);
Knob.transform.position = Background.position;
}
public override void onTouchMove(Touch pos)
{
PointPosition = new Vector2((pos.position.x - Background.position.x) / ((Background.rect.size.x - Knob.rect.size.x) / 2), (pos.position.y - Background.position.y) / ((Background.rect.size.y - Knob.rect.size.y) / 2));
PointPosition = (PointPosition.magnitude > 1.0f) ? PointPosition.normalized : PointPosition;
Knob.transform.position = new Vector2((PointPosition.x * ((Background.rect.size.x - Knob.rect.size.x) / 2) * offset) + Background.position.x, (PointPosition.y * ((Background.rect.size.y - Knob.rect.size.y) / 2) * offset) + Background.position.y);
}
public JoyCam joyCam;
public Transform tag;
public float angle;
public float angle1;
public bool isMove;
public Vector3 ve3;
public static float PointToAngle(Vector2 p1, Vector2 p2)
{
float angle = Mathf.Atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Mathf.PI;
if (angle >= 0 && angle <= 180)
{
return angle;
}
else
{
return 360 + angle;
}
}
private void FixedUpdate()
{
Horizontal = PointPosition.x;
Vertical = PointPosition.y;
isMove = Mathf.Abs(Vertical - 0) > 0.2f || Mathf.Abs(Horizontal - 0) > 0.2f;
joyCam.isMove = isMove;
if (isMove)
{
Horizontal *= -1;
Vertical *= -1;
angle = PointToAngle(new Vector2(Vertical, Horizontal), Vector2.zero) - angle1 + Camera.main.transform.localEulerAngles.y;
tag.transform.localEulerAngles = new Vector3(0, angle, 0);
ve3 = Vector3.forward * 5f * Time.deltaTime;
tag.transform.Translate(ve3);
}
}
}
JoyCam.cs? 控制摄像机?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JoyCam : JoyStickButton
{
public GameObject camTag;
public GameObject target;
public Vector3 offset = new Vector3(0, 1.6f, -3f);
public bool isSelf = false;
public bool isMove = false;
public bool isRotating = false;
public float distance = 0f;
public float scrollSpeed = 0.5f;
public float rotateSpeed = 2;
public float mousex;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (isMove)
{
camTag.transform.localPosition = target.transform.localPosition + offset;
}
if (isRotating && isMove)
{
target.transform.localEulerAngles = new Vector3(target.transform.localEulerAngles.x, target.transform.localEulerAngles.y + mousex, target.transform.localEulerAngles.z);
}
camTag.transform.LookAt(new Vector3(target.transform.localPosition.x, target.transform.localPosition.y + 1f, target.transform.localPosition.z));
Debug.Log("isRotating " + isRotating + " isMove " + isMove);
}
public override void onTouchBegan(Touch touch)
{
isRotating = false;
}
public override void onTouchEnd(Touch touch)
{
isRotating = false;
}
public override void onTouchMove(Touch touch)
{
isRotating = true;
distance = offset.magnitude;
distance += touch.deltaPosition.y * -scrollSpeed;
distance = Mathf.Clamp(distance, 1.0f, 7.0f);
offset = offset.normalized * distance;
camTag.transform.localPosition = target.transform.localPosition + offset;
mousex = touch.deltaPosition.x * rotateSpeed;
camTag.transform.RotateAround(target.transform.localPosition, target.transform.up, mousex);
offset =camTag.transform.localPosition - target.transform.localPosition;
}
}
主要以移动端?操作为主
https://download.csdn.net/download/a1228267639/83928895?项目源码下载
|