直接上代码通过使用Vector3.Reflect方法实现游戏物体的反射
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
private Vector3 dir = new Vector3(-1,1,0).normalized;
void Start () {
}
// Update is called once per frame
void Update ()
{
transform.Translate(dir * Time.deltaTime * 2);
}
void OnCollisionEnter(Collision other)
{
if (other.collider.tag == "UpWall")
{
Debug.Log(0);
dir = Vector3.Reflect(dir, Vector3.up);
}
else if (other.collider.tag == "RightWall")
{
Debug.Log(0);
dir = Vector3.Reflect(dir, Vector3.right);
}
}
}
使用检测碰撞来触发相关的事件 特别注意的是碰撞器和触发器的区别,这个度娘有总结
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Power : MonoBehaviour {
Vector3 force =new Vector3 (0.5f,0,0.1f);
void Start () {
}
// Update is called once per frame
void Update () {
if (Gui.star )
{
AddForec(force);
}
}
void AddForec( Vector3 force)
{
Rigidbody bulletRig = GetComponent<Rigidbody>();
bulletRig.AddForce(force * 10, ForceMode.Force );
}
void OnCollisionEnter(Collision collision)
{
if (collision.transform.CompareTag("Player1"))
{
Debug.Log(2);
force = Vector3.Reflect(force , Vector3.right );
}
if (collision.transform.CompareTag("Player2"))
{
Gui .player1++;
Application.LoadLevel(0);
// force = Vector3.Reflect(force, Vector3.left);
Destroy(gameObject);
}
if (collision.transform.CompareTag("Boll"))
{
Gui . player2++;
Application.LoadLevel(0);
Destroy(gameObject);
//force = Vector3.Reflect(force, Vector3.right );
}
if (collision.transform.CompareTag("Boll1"))
{
Debug.Log(1);
force = Vector3.Reflect(force, Vector3.forward);
}
}
}
刚开始使用CSDN操作不熟悉直接上源码链接:http://pan.baidu.com/s/1jIMiWZk 密码:ktlh