Uinty3D 一个动画播放以及射线查询的Demo

Uinty3D 一个动画播放以及射线查询的Demo_第1张图片

 

//以下是实现代码

using UnityEngine;
using System.Collections;

public class SampleMoveScriptC : MonoBehaviour {
 
 public float speed = 3.0f;
 public float rotatationSpeed = 200.0f;
 private float curSpeed = 0.0f;
 
 private GameObject obj = null;
 private Vector3 point;


 // Use this for initialization
 void Start () {
  obj = GameObject.Find("robot");
 }
 
 // Update is called once per frame
 void Update () {
  
  if(Input.GetMouseButtonDown(0))//Down或Up
     {
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);        
         RaycastHit hit;
         if(Physics.Raycast(ray,out hit))
         { 
          if(hit.collider.gameObject.name=="Cube")//射线碰撞到的物体名称
          {  
          point = hit.point;    
     obj.transform.LookAt(new Vector3(point.x, obj.transform.position.y, point.z));
 
          }
   }
     }
 
  float newSpeed = Input.GetAxis("Vertical") * speed;
  if (Input.GetKey("left shift"))
  {
   newSpeed *= 1.5f;
  }
  float distance = Vector3.Distance(point, transform.position);
  CharacterController controller = obj.GetComponent<CharacterController>();
  Vector3 forward = Vector3.ClampMagnitude(point - obj.transform.position, 1.5f);
  if(Mathf.Abs(distance) > 1.45f && controller)
  {
   
   obj.animation.CrossFade("run");
   controller.SimpleMove(forward * 1.5f);
  }
  else
  {
   obj.animation.CrossFade("idle");
  }

 }

}


 

 

你可能感兴趣的:(Uinty3D 一个动画播放以及射线查询的Demo)