5.2用搖桿控制物體的移動

ImageBG腳本

usingUnityEngine;

usingSystem.Collections;

usingUnityEngine.UI;

//不手動添加ScrollRect組件,讓腳本繼承與ScrollRect,從而獲得ScrollRect的屬性、方法

public class ScrollCricle:ScrollRect{

private float? mRadius;

private Vector3?offsetVector3=Vector3.zero;//搖桿的偏移

protected override void? Start( ){

base.Start( );

//計算搖桿移動半徑

mRadius=(transform? as RectTransform).sizeDelta.x*0.5f;

}

voidUpdate( ){

CalculateOffect();

}

public override void OnDrag(UnityEngine.EventSystems.PointerEventData?eventData){

//獲得父類屬性

base.OnDrag(eventData);

base.content.anchoredPosition=Vector3.ClampMagnitude(base.content.anchoredPosition,mRadius);

}

//計算偏移

privatevoidCalculateOffect( ){

//(-1,1)~(1,1)

offsetVector3=content.localPosition/mRadius;

}

public Vector3?GetoffsetVector3( ){

return offsetVector3;

}

}

被控制物體腳本

usingUnityEngine;

usingSystem.Collections;

usingSystem.Collections.Generic;

public class CubeMove:MonoBehaviour{

public ScrollCricle?scrollCricle;//獲得Scroll腳本

voidStart( ){

}

voidUpdate( ){

Vector3?offset=scrollCricle.GetoffsetVector3();

//物體的朝向隨著搖桿的方向移動(四元數(shù))

transform.rotation=Quaternion.LookRotation(new Vector3(offset.x,0,offset.y));



transform.position+=newVector3(offset.x,0,offset.y)*10*Time.deltaTime;

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容