Unity3D_脚本_获取对象的位置_碰撞后加一段音乐_旋转对象_使物体随机运动

获取对象的位置(Position)

在代码中加上

public Rigidbody cd;
cd = GetComponent<Rigidbody>();
Vector3 m=cd.transform.position;
1
2
3
m[0]为y轴世界坐标
m[1]为y轴世界坐标
m[2]为y轴世界坐标

碰撞后加一段音乐


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

public class Sound : MonoBehaviour
{

//播放音乐
public AudioClip[] bg_sounds;
private AudioSource audio_source;

void Awake ()
{
audio_source = GetComponent<AudioSource> ();
audio_source.volume = 1;
audio_source.clip = bg_sounds [0];
audio_source.Play();
}

// Update is called once per frame
void Update ()
{


}
void OnCollisionEnter(Collision collison)
{
audio_source.clip = bg_sounds[1];
audio_source.Play();
Application.LoadLevel("endGame");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
旋转对象
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotator : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
使物体随机运动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rend : MonoBehaviour {
public float speed;

private float moveSpeed=4;//移动速度.

void Start () {
//设置初始位置.
transform.position=new Vector3(5,0,0);
}
// Update is called once per frame
void Update () {
if(transform.position.x>-22){
//移动.
transform.Translate(Vector3.right*-moveSpeed*Time.deltaTime);
}
else{
transform.position=new Vector3(5,0,0);
}
}
}
--------------------- 

转载于:https://www.cnblogs.com/ly570/p/10971093.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值