Shark Deng

Interactive Designer @Shanju Design in Shanghai

touched caffe / mxnet / python / wordpress / bootstrap / Arduino / Raspberry Pi / webgl / three.js / PIXI / 3dMax

Email

Github

Unity – 让物体紧贴地面移动

2019-11-20 17:39:34

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DetectGround : MonoBehaviour {

	private float boxCol;

	// Use this for initialization
	void Start () {
		boxCol = gameObject.GetComponent<BoxCollider> ().bounds.size.y;

	}
	
	// Update is called once per frame
	void Update () {
		Ray ray = new Ray (transform.position, -transform.up);
		RaycastHit hit;

		if (Physics.Raycast(ray, out hit)){
			Debug.Log("hello");
			transform.position = new Vector3(transform.position.x, hit.point.y + boxCol / 2, transform.position.z);
			transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
		}
	}
}

 



——By SharkDeng
如果你喜欢我的文章,欢迎红包赞赏

Leave a Reply

Your email address will not be published. Required fields are marked *