ROLL-A-BALL TUTORIAL #4
Way to move the camera
1. Create a component which traces “Player”
It creates a component which trace “Player”. This component has some function following.
It creates a component which trace “Player”. This component has some function following.
So, at first, it creates the “Follow Player” component. Then add “Main Camera” to it.
Next, double click “FollowPlayer” to invoke code editor.
2. Way to trace player
It makes tracing function from getting an object’s position. Write a source code below in “FollowPlayer.cs”.
FollowPlayer.csusing UnityEngine;
using System.Collections;
public class FollowPlayer : MonoBehaviour
{
public Transform target; // Reference to target
void Update ()
{
// set target’s position to oneself
GetComponent<Transform>().position = target.position;
}
}
Look component of “Main Camera” and confirm what add term of target in “Follow Player”. Drag & drop “Player” onto this term.
Let’s start the game. Game View shows a view like the below picture as FPS.
3. Set offset to a component
Change a camera point like TPS that have an appropriate distance to ball. So, add a function which maintains the distance between “Player” and “Main Camera” when starting the game. The function is created by below sub-functions.
using UnityEngine;
using System.Collections;
public class FollowPlayer : MonoBehaviour
{
public Transform target; // Reference to target
void Update ()
{
// set target’s position to oneself
GetComponent<Transform>().position = target.position;
}
}
Change a camera point like TPS that have an appropriate distance to ball. So, add a function which maintains the distance between “Player” and “Main Camera” when starting the game. The function is created by below sub-functions.
At first, Set the position and rotation to “Main Camera” like a below picture.
Next, Change a source code.
FollowPlayer.cs
using UnityEngine;
using System.Collections;
public class FollowPlayer : MonoBehaviour
{
public Transform target; // Reference to target
private Vector3 offset; // relative distance
void Start ()
{
// Get relative distance between “Player” and “Main Camera”
offset = GetComponent<Transform>().position - target.position;
}
void Update ()
{
// Set “Player’s position + relative distance” to oneself
GetComponent<Transform>().position = target.position + offset;
}
Let's play the game.
Reference
이 문제에 관하여(ROLL-A-BALL TUTORIAL #4), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/amex/items/52293fe1eb75b3a76fe3텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)