简体   繁体   English

团结-不断改变游戏对象的前进方向?

[英]Unity - Continuously change the forward direction of game object?

I'm creating a project in which I have a sphere GameObject which has empty game object as a child. 我正在创建一个项目,其中有一个球体GameObject,其中的子对象为空游戏对象。 A line renderer component is attached to the this empty game object. 线条渲染器组件已附加到此空游戏对象。

I have made the sphere to look at the touch direction so that the line renderer of the child object also faces towards the touch position. 我制作了球体来查看触摸方向,以便子对象的线条渲染器也面向触摸位置。 It works fine when the sphere is rotating in one axis. 当球体沿一个轴旋转时,它可以正常工作。

But the problem occurs when I apply a force to the sphere and it starts rotating in all axis just like a normal ball. 但是,当我向球体施加力并且像普通球一样开始在所有轴上旋转时,就会出现问题。 But the line renderer attached to it is stuck to its face and when the sphere faces downwards, the line renderer goes into the ground. 但是连接到它的线条渲染器粘贴在其表面上,并且当球体朝下时,线条渲染器进入地面。

Code for Line Renderer. Line Renderer的代码。

using UnityEngine;
using System.Collections;

public class LineRenderer : MonoBehaviour {

private LineRenderer lineRenderer;

private Vector3 finalTouchPosition;

// Use this for initialization
void Start () {

    lineRenderer = gameObject.GetComponent<LineRenderer> ();
}



// Update is called once per frame
void Update () {

    Ray ray = new Ray (transform.position, transform.forward);

    RaycastHit hit;

    lineRenderer.SetPosition (0, ray.origin);


    if (Physics.Raycast (ray, out hit)) {
        lineRenderer.SetPosition (1, hit.point);    
    }


}

} }

Code for rotating the object with touch. 触摸旋转对象的代码。

if(theTouch.phase == TouchPhase.Moved)
    {

    Vector3 tempTouch = new Vector3(theTouch.position.x ,theTouch.position.y, myCamera.position.y - targetObject.transform.position.y);

    fingerPosition = Camera.main.ScreenToWorldPoint(tempTouch);

    targetObject.transform.LookAt(fingerPosition);

    isRotating = true;

    }

I want the line renderer to always be parallel to the ground but originate from the sphere or face of the sphere. 我希望线渲染器始终与地面平行,但源于球体或球体的面。 I've tried many things. 我已经尝试了很多东西。 But still haven't found the solution. 但是仍然没有找到解决方案。

Your question seems a little confusing, but it seems like you want a ball to rotate around a line. 您的问题似乎有点令人困惑,但似乎您希望球绕直线旋转。 The ball to rotate at its own will and the line to just stay relative to the ball and no rotation. 球按照自己的意愿旋转,并且直线仅相对于球保持不旋转。

If this is correct. 如果正确的话。 What I suggest is having two objects. 我建议有两个对象。 the ball and then an empty/invis object. 球,然后是一个空/隐形物体。 The ball rotates and moves and just takes the empty object with it by movement, no rotation. 球旋转并移动,只是通过移动来带走空物体,不旋转。 Put the line renderer on the empty object and in the hierarchy have invis object be the parent of the ball. 将线条渲染器放在空对象上,然后在层次结构中将invis对象作为球的父对象。 Move the empty object with the ball physics and bamn rolling ball, that responds to touch. 用球物理原理和空球移动空物体,以响应触摸。

Sorry, if Im bad at explaining. 对不起,如果我不好讲。 And if you dont know when you parent something in the hierarchy they move together. 而且,如果您不知道何时将层次结构中的某项作为父项,它们会一起移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM