I got an object at the screen. At the beginning it is located at x=0,y=0,z=0 later it moves to a different position. I just want to ma开发者_如何学JAVAke sure that this object does not move to locations invisible to the user (it's the player main character itself and I neither want the player to be able to go back from his current location nor going forward the camera following him).
Can you explain to me what I need to do here?
I think you're asking:
If a player-character on the screen is moving around, you want the person sitting at the computer to be able to see it always. To do this you need to set up the camera to follow this object.
Here are some links that may get you started:
http://unity3d.com/support/documentation/Components/class-Camera.html
http://unity3d.com/support/documentation/ScriptReference/Camera.html
This tutorial has an example of how to implement a camera that follows the player.
http://unity3d.com/support/resources/tutorials/3d-platform-game.html
using UnityEngine;
using System.Collections;
public class NewGameScript : MonoBehaviour
{
float t=0f;
float v=20f;
float dist;
// Use this for initialization
void Start ()
{
}
void Update ()
{
if(Input.GetKeyUp("up"))
{
if(t<0.1)
{
t +=Time.deltaTime*0.07f;
}
dist=t*v*0.5f;
gameObject.transform.position +=transform.forward*dist;
}
else if(Input.GetKeyUp("down"))
{
if(t>-0.1)
{
t -=Time.deltaTime*0.07f;
}
dist=t*v*0.5f;
gameObject.transform.position +=transform.forward*dist;
}
else if(Input.GetKey("left"))
{
transform.Rotate(0,-0.9f*(dist+1), 0);
gameObject.transform.position +=transform.forward*0.1f*(dist+1);
}
else if(Input.GetKey("right"))
{
transform.Rotate( 0,0.9f*(dist+1),0);
gameObject.transform.position +=transform.forward*0.1f*(dist+1);
}
}
}
精彩评论