Unity 5.1 播放器控制次要对象相对于播放器对象
Unity 5.1 player control of secondary object relative to player object
我做了很多搜索,但找不到解决这个问题的方法。
我想要的是控制一个游戏对象,该对象基于右模拟摇杆输入以固定距离围绕玩家对象旋转,与玩家移动无关。我还希望物体在玩家周围移动时面朝外。
我已将左模拟摇杆设置为播放器移动和工作,并已设置并测试右模拟摇杆,因此我知道输入正在工作。
我尝试了 transform.rotate、RotateAround、.position 和四元数等,但无法弄清楚或找不到任何可能有帮助的方法。我对此很陌生,所以可能有一个简单的解决方案,我只是看不到它!
感谢您可能提供的任何帮助:) 非常感谢
编辑 2:第二次尝试
到目前为止我已经知道了:
public class ShieldMovement : MonoBehaviour {
public Transform target; //player shield is attaced to
float distance = 0.8f; // distance from player so it doesn't clip
Vector3 direction = Vector3.up;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float angle = Mathf.Atan2 (Input.GetAxisRaw("rightH"), Input.GetAxisRaw("rightV"))* Mathf.Rad2Deg;
if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
{
direction = new Vector3(Input.GetAxis("rightH"),Input.GetAxis("rightV"), 0.0f ) ;
}
Ray ray = new Ray(target.position, direction);
transform.position = ray.GetPoint(distance);
if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
{
transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward*-1);
}
}
}
我希望护盾围绕玩家平滑旋转到新位置,而不是传送到那里。我已经尝试了一些 lerps 和一些 slerps,到目前为止,我所能做的就是从圆周上的一个点沿直线插值到新点。想不出一种方法让它围绕玩家旋转,就像你只旋转摇杆一样。希望这是有道理的!
大家有什么好主意吗?
这里有两种方法可以完成您想要的
请注意:这还没有经过测试
使用自定义规范化进程(0f
正好是 Vector3.forward
,1f
带你回到 Vector3.forward
)
using UnityEngine;
using System.Collections;
public class Follower : MonoBehaviour {
float distance = 10f;
Vector3 direction = Vector3.forward;
float procession = 0f; // exactly at world forward 0
Transform target;
void Update() {
float circumference = 2 * Mathf.PI * distance;
angle = (procession % 1f) * circumference;
direction *= Quaternion.AngleAxis(Mathf.Rad2Deg * angle, Vector3.up);
Ray ray = new Ray(target.position, direction);
transform.position = ray.GetPoint(distance);
transform.LookAt(target);
}
}
和Input.GetAxis
using UnityEngine;
using System.Collections;
public class Follower : MonoBehaviour {
float distance = 10f;
Vector3 direction = Vector3.forward;
float speed = .01f;
Transform target;
void Update() {
direction *= Quaternion.AngleAxis(Input.GetAxis("Horizontal") * speed * Time.deltaTime, Vector3.up);
Ray ray = new Ray(target.position, direction);
transform.position = ray.GetPoint(distance);
transform.LookAt(target);
}
}
我做了很多搜索,但找不到解决这个问题的方法。
我想要的是控制一个游戏对象,该对象基于右模拟摇杆输入以固定距离围绕玩家对象旋转,与玩家移动无关。我还希望物体在玩家周围移动时面朝外。
我已将左模拟摇杆设置为播放器移动和工作,并已设置并测试右模拟摇杆,因此我知道输入正在工作。
我尝试了 transform.rotate、RotateAround、.position 和四元数等,但无法弄清楚或找不到任何可能有帮助的方法。我对此很陌生,所以可能有一个简单的解决方案,我只是看不到它!
感谢您可能提供的任何帮助:) 非常感谢
编辑 2:第二次尝试
到目前为止我已经知道了:
public class ShieldMovement : MonoBehaviour {
public Transform target; //player shield is attaced to
float distance = 0.8f; // distance from player so it doesn't clip
Vector3 direction = Vector3.up;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float angle = Mathf.Atan2 (Input.GetAxisRaw("rightH"), Input.GetAxisRaw("rightV"))* Mathf.Rad2Deg;
if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
{
direction = new Vector3(Input.GetAxis("rightH"),Input.GetAxis("rightV"), 0.0f ) ;
}
Ray ray = new Ray(target.position, direction);
transform.position = ray.GetPoint(distance);
if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
{
transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward*-1);
}
}
}
我希望护盾围绕玩家平滑旋转到新位置,而不是传送到那里。我已经尝试了一些 lerps 和一些 slerps,到目前为止,我所能做的就是从圆周上的一个点沿直线插值到新点。想不出一种方法让它围绕玩家旋转,就像你只旋转摇杆一样。希望这是有道理的!
大家有什么好主意吗?
这里有两种方法可以完成您想要的
请注意:这还没有经过测试
使用自定义规范化进程(0f
正好是 Vector3.forward
,1f
带你回到 Vector3.forward
)
using UnityEngine;
using System.Collections;
public class Follower : MonoBehaviour {
float distance = 10f;
Vector3 direction = Vector3.forward;
float procession = 0f; // exactly at world forward 0
Transform target;
void Update() {
float circumference = 2 * Mathf.PI * distance;
angle = (procession % 1f) * circumference;
direction *= Quaternion.AngleAxis(Mathf.Rad2Deg * angle, Vector3.up);
Ray ray = new Ray(target.position, direction);
transform.position = ray.GetPoint(distance);
transform.LookAt(target);
}
}
和Input.GetAxis
using UnityEngine;
using System.Collections;
public class Follower : MonoBehaviour {
float distance = 10f;
Vector3 direction = Vector3.forward;
float speed = .01f;
Transform target;
void Update() {
direction *= Quaternion.AngleAxis(Input.GetAxis("Horizontal") * speed * Time.deltaTime, Vector3.up);
Ray ray = new Ray(target.position, direction);
transform.position = ray.GetPoint(distance);
transform.LookAt(target);
}
}