【问题标题】:Unity 5.1 player control of secondary object relative to player objectUnity 5.1 玩家控制次要对象相对于玩家对象
【发布时间】:2015-11-20 06:15:42
【问题描述】:

我已经做了很多搜索,但找不到解决这个问题的方法。

我想要的是控制一个游戏对象,它根据正确的模拟摇杆输入以固定距离围绕玩家对象旋转,与玩家运动无关。我还希望对象在玩家周围移动时朝外。

我已将模拟摇杆设置为播放器移动并正常工作,并设置并测试了右侧模拟摇杆,因此我知道输入正常。

我尝试过 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,到目前为止,我所能做的就是从圆周上的一个点沿直线插入到新点。想不出一种方法让它围绕玩家旋转,就像你只是旋转摇杆一样。希望这是有道理的!

你有什么很棒的想法吗?

【问题讨论】:

  • 您是否已经查看过 SmoothFollow 相机脚本?它使摄像机以一定的角度和距离跟随玩家旋转以面对他。您也可以调整跟随对象,并在移动摇杆时增加位置(x,0,z)。我现在无法对其进行测试,但查看脚本似乎是最终了解您需要做什么的好方法......
  • 我会检查一下,谢谢 :) 如果有效,将发布 :)
  • 检查了一下,有点明白它的来源,但我似乎无法理解。如果你想看看我到目前为止得到了什么,我已经发布了一个没有上面动画的部分解决方案(因为我无法让它工作):)

标签: c# unity3d rotation position


【解决方案1】:

这里有两种方法可以实现你想要的

请注意:这还没有经过测试

使用自定义标准化队列0f 正好是 Vector3.forward1f 带您回到 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);
    }
}

【讨论】:

  • 对我来说都不太好用,但是我得到了第二个没有动画部分的工作,当你在棒上指向那个方向时它就会出现在正确的位置,会发布我的代码
  • 就像我说的,还没有测试过,但我确定解决方案在代码中的某个地方,并进行了一些小的调整;)
  • 是的,非常有帮助,谢谢 :) 我还在努力研究它 :)
猜你喜欢
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
相关资源
最近更新 更多