私は生存ゲームを作っています...私はいくつかの動きをしましたが、今はキャラクターをジャンプさせて、その上にプラットフォームをつかみ、そのプラットフォームを歩いて...そしてまた私は泳ぐことを望む、私は野生のゼルダの呼吸でちょっと泳いでいるのを見た。モータスクリプト私のプレイヤーをユニティで泳ぐ、掴む、持ち上げる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Jump : MonoBehaviour {
private CharacterController jumpControl;
private float gravity = 7f, jumpForce = 5f;
private float verticalVelocity;
NavMeshAgent agent;
PlayerMotor motor;
// Use this for initialization
void Start() {
jumpControl = GetComponentInChildren<CharacterController>();
agent = GetComponentInParent<NavMeshAgent>();
}
// Update is called once per frame
void FixedUpdate() {
if (jumpControl.isGrounded) {
verticalVelocity = -gravity * Time.deltaTime;
agent.enabled = true;
if (Input.GetMouseButtonDown (1)) {
agent.enabled = false;
verticalVelocity = jumpForce;
Vector3 moveVector = new Vector3 (0, verticalVelocity, 0);
jumpControl.Move (moveVector * Time.deltaTime);
}
} else {
verticalVelocity -= gravity * Time.deltaTime;
Vector3 moveVector = new Vector3 (0, verticalVelocity, 0);
jumpControl.Move (moveVector * Time.deltaTime);
}
}
}
その後:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class PlayerMotor : MonoBehaviour {
Transform target;
public NavMeshAgent agent;
// Use this for initialization
void Start() {
agent = GetComponent<NavMeshAgent>();
}
void Update(){
if (target != null) {
agent.SetDestination (target.position);
FaceTarget();
}
}
public void MoveToPoint(Vector3 point){
agent.SetDestination (point);
}
public void FollowTarget(Interactables newTarget){
agent.stoppingDistance = newTarget.radius * .8f;
agent.updateRotation = false;
target = newTarget.interactionTransform;
}
public void StopFollowingTarget(){
agent.stoppingDistance = 0f;
agent.updateRotation = true;
target = null;
}
void FaceTarget(){
Vector3 direction = (target.position -
transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation (new Vector3
(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp (transform.rotation,
lookRotation, Time.deltaTime * 5f);
}
}