Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StockRunning
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
czy
StockRunning
Commits
716504db
Commit
716504db
authored
Apr 22, 2021
by
czy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AI躲避障碍物
parent
9343f4aa
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
218 additions
and
29 deletions
+218
-29
#B2_Graphics.meta
StockRunning/Assets/#B2_Graphics.meta
+8
-0
#Z1_Tabs.meta
StockRunning/Assets/#Z1_Tabs.meta
+8
-0
#Z2_Documents.meta
StockRunning/Assets/#Z2_Documents.meta
+8
-0
#Z3_WebAPI.meta
StockRunning/Assets/#Z3_WebAPI.meta
+8
-0
#Z4_Icons.meta
StockRunning/Assets/#Z4_Icons.meta
+8
-0
#Z4_Previews.meta
StockRunning/Assets/#Z4_Previews.meta
+8
-0
AIMove.cs
StockRunning/Assets/My/Scripts/AI/AIMove.cs
+135
-26
Prop.cs
StockRunning/Assets/My/Scripts/Prop/Prop.cs
+19
-3
New Terrain 1.asset
StockRunning/Assets/New Terrain 1.asset
+0
-0
New Terrain 1.asset.meta
StockRunning/Assets/New Terrain 1.asset.meta
+8
-0
New Terrain.asset
StockRunning/Assets/New Terrain.asset
+0
-0
New Terrain.asset.meta
StockRunning/Assets/New Terrain.asset.meta
+8
-0
No files found.
StockRunning/Assets/#B2_Graphics.meta
0 → 100644
View file @
716504db
fileFormatVersion: 2
guid: 317d19313d1d7204e9bbc37122116e7f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
StockRunning/Assets/#Z1_Tabs.meta
0 → 100644
View file @
716504db
fileFormatVersion: 2
guid: ca40beb7fc6998f40a6e15a59c6ab599
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
StockRunning/Assets/#Z2_Documents.meta
0 → 100644
View file @
716504db
fileFormatVersion: 2
guid: 325c11e064841204bbd6de7c85bebef6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
StockRunning/Assets/#Z3_WebAPI.meta
0 → 100644
View file @
716504db
fileFormatVersion: 2
guid: aa43ce62596b0784081ec9bd0abc8516
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
StockRunning/Assets/#Z4_Icons.meta
0 → 100644
View file @
716504db
fileFormatVersion: 2
guid: 2e117c0bfc6741e4ebc4a8c8aebc0b8a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
StockRunning/Assets/#Z4_Previews.meta
0 → 100644
View file @
716504db
fileFormatVersion: 2
guid: 55c4962f2a6593c4b9c1bb6003b1ff08
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
StockRunning/Assets/My/Scripts/AI/AIMove.cs
View file @
716504db
...
...
@@ -4,6 +4,12 @@ using UnityEngine;
using
DG.Tweening
;
using
DG.Tweening.Core
;
public
enum
AIStrategy
{
StraightLine
=
1
,
//直线
PickUpPropsAndGold
=
2
,
//拾取道具和金币
AvoidObstaclesAndPickUp
=
3
//躲避障碍物 并拾取道具和金币
}
public
class
AIMove
:
MonoBehaviour
{
[
Header
(
"控制移动"
)]
...
...
@@ -18,11 +24,12 @@ public class AIMove : MonoBehaviour
[
Header
(
"地面检测"
)]
public
Transform
ray1Pos
;
public
Transform
ray2Pos
;
public
Transform
raySeeWayPos
;
[
Header
(
"AI策略"
)]
public
AIStrategy
aIStrategy
;
private
RaycastHit
RHit
;
private
CharacterController
characterController
;
private
bool
m_bIsCanMove
=
true
;
//是否能开车
private
MeshCollider
[]
m_targetTrans
;
//所有目标
private
MeshCollider
m_curTargetTrans
;
//当前抓的目标
...
...
@@ -40,6 +47,13 @@ public class AIMove : MonoBehaviour
//控制移动的中间变量
private
float
SpeedY
=
0
;
//相当于重力
private
float
deltaX
=
0.0f
;
//左右移动的距离。
private
int
AvoidObstaclesState
=
0
;
//0==未开始 1==躲避中 2==正在经过障碍物两侧。
//private float minX = 0;
//private float maxX = 0;
private
float
maxZ
=
-
10000
;
private
float
timerT1
=
0.0f
;
private
float
avoidX
=
0.0f
;
private
int
AvoidDir
=
0
;
//-1 0 1 左 不躲避 右
...
...
@@ -189,11 +203,11 @@ public class AIMove : MonoBehaviour
if
(
targetObj
==
null
)
{
targetObj
=
GetTargetPos
();
print
(
"获取新目标:"
+
targetObj
);
//
print("获取新目标:"+ targetObj);
}
else
if
(
targetObj
.
transform
.
position
.
z
<
transform
.
position
.
z
)
{
targetObj
=
GetTargetPos
();
print
(
"获取新目标:"
+
targetObj
);
//
print("获取新目标:" + targetObj);
}
...
...
@@ -208,29 +222,29 @@ public class AIMove : MonoBehaviour
if
(!
Grounded
)
{
SpeedY
+=
-
9.8f
*
5
*
Time
.
deltaTime
;
SpeedY
+=
-
9.8f
*
5
*
Time
.
deltaTime
;
}
else
//AI策略,需要每一帧都执行放到FixedUpdate中。
switch
(
aIStrategy
)
{
//在地面上,计算导航去目标位置,每次移动的距离。
if
(
targetObj
!=
null
)
{
if
(
targetObj
.
transform
.
position
.
x
-
transform
.
position
.
x
>
0.3f
)
//误差值0.3
{
deltaX
=
HSpeed
*
Time
.
deltaTime
;
}
else
if
(
targetObj
.
transform
.
position
.
x
-
transform
.
position
.
x
<
-
0.3f
)
{
deltaX
=
-
HSpeed
*
Time
.
deltaTime
;
}
else
{
deltaX
=
0
;
}
}
case
AIStrategy
.
StraightLine
:
DoStraightLine
();
break
;
case
AIStrategy
.
PickUpPropsAndGold
:
DoPickUpPropsAndGold
();
break
;
case
AIStrategy
.
AvoidObstaclesAndPickUp
:
DoPickUpPropsAndGold
();
DoAvoidObstaclesAndPickUp
();
break
;
default
:
DoStraightLine
();
break
;
}
//射线检测。
//Debug.DrawRay(transform.position, Vector3.down * 10f, Color.red);
RaycastHit
hit
;
...
...
@@ -287,12 +301,107 @@ public class AIMove : MonoBehaviour
timerVertigo
-=
Time
.
deltaTime
;
}
}
}
//AI策略,需要每一帧都执行放到FixedUpdate中。
public
void
DoStraightLine
()
{
deltaX
=
0
;
}
//AI策略,需要每一帧都执行放到FixedUpdate中。
public
void
DoPickUpPropsAndGold
()
{
if
(!
Grounded
)
{
}
else
{
//在地面上,计算导航去目标位置,每次移动的距离。
if
(
targetObj
!=
null
)
{
if
(
targetObj
.
transform
.
position
.
x
-
transform
.
position
.
x
>
0.3f
)
//误差值0.3
{
deltaX
=
HSpeed
*
Time
.
deltaTime
;
}
else
if
(
targetObj
.
transform
.
position
.
x
-
transform
.
position
.
x
<
-
0.3f
)
{
deltaX
=
-
HSpeed
*
Time
.
deltaTime
;
}
else
{
deltaX
=
0
;
}
}
}
}
//AI策略,需要每一帧都执行放到FixedUpdate中。
public
void
DoAvoidObstaclesAndPickUp
()
{
Debug
.
DrawRay
(
raySeeWayPos
.
position
,
raySeeWayPos
.
forward
*
10f
,
Color
.
red
);
RaycastHit
seeHit
;
bool
isSee
=
Physics
.
Raycast
(
raySeeWayPos
.
position
,
raySeeWayPos
.
forward
,
out
seeHit
,
6.0f
);
if
(
seeHit
.
collider
!=
null
)
{
//print("看见物体:" + seeHit.collider.tag);
if
(
seeHit
.
collider
.
tag
==
"Prickle"
)
{
//前方有障碍物
//deltaX = 0;//将寻金币的横向的位移变为0;
//seeHit.collider.bounds.size.x
//seeHit.transform.position.x - seeHit.collider.bounds.size.x * 0.5f
//minX = seeHit.transform.position.x - seeHit.collider.bounds.size.x * 0.5f - 0.5f;
//maxX = seeHit.transform.position.x - seeHit.collider.bounds.size.x * 0.5f + 0.5f;
maxZ
=
seeHit
.
transform
.
position
.
z
+
seeHit
.
collider
.
bounds
.
size
.
z
*
0.5f
+
0.5f
;
if
(
transform
.
position
.
x
<
seeHit
.
transform
.
position
.
x
)
{
//去左边
avoidX
=
seeHit
.
transform
.
position
.
x
-
seeHit
.
collider
.
bounds
.
size
.
x
*
0.5f
-
0.5f
;
AvoidDir
=
-
1
;
}
else
{
//去右边
avoidX
=
seeHit
.
transform
.
position
.
x
+
seeHit
.
collider
.
bounds
.
size
.
x
*
0.5f
+
0.5f
;
AvoidDir
=
1
;
}
}
}
if
(
transform
.
position
.
z
<
maxZ
)
{
//print("前方右障碍物,开始躲避AvoidDir:"+ AvoidDir+ "transform.position.x:"+ transform.position.x + "avoidX:" + avoidX);
//前方右障碍物,开始躲避。
//关闭金币导航
deltaX
=
0
;
//开始左右躲避
if
(
AvoidDir
==
-
1
&&
transform
.
position
.
x
>=
avoidX
)
{
//向左移动
//print("向左移动");
deltaX
=
-
HSpeed
*
Time
.
deltaTime
;
}
else
if
(
AvoidDir
==
-
1
&&
transform
.
position
.
x
<
avoidX
)
{
//向左躲避成功
AvoidDir
=
0
;
}
else
if
(
AvoidDir
==
1
&&
transform
.
position
.
x
<=
avoidX
)
{
//向右走
deltaX
=
HSpeed
*
Time
.
deltaTime
;
//print("向右移动");
}
else
if
(
AvoidDir
==
1
&&
transform
.
position
.
x
>
avoidX
)
{
//躲避成功
AvoidDir
=
0
;
}
}
}
//摇杆控制
void
UpdateJoystick
()
{
...
...
StockRunning/Assets/My/Scripts/Prop/Prop.cs
View file @
716504db
...
...
@@ -14,7 +14,8 @@ public class Prop : MonoBehaviour
public
float
disZ
=
50
;
public
Material
Enemy_VertigoMat
;
[
Header
(
"巨石"
)]
public
GameObject
giantRockObj
;
//public GameObject giantRockObj;
public
List
<
GameObject
>
rockList
;
public
Material
GiantRockMat
;
...
...
@@ -63,7 +64,7 @@ public class Prop : MonoBehaviour
// Destroy(gameObject);
//}
}
public
void
ShowEnemyAI
()
{
//print("出现打手。");
...
...
@@ -75,7 +76,22 @@ public class Prop : MonoBehaviour
public
void
ShowGiantRock
()
{
//print("出现巨石。");
giantRockObj
.
SetActive
(
true
);
FilterRock
();
int
index
=
Random
.
Range
(
0
,
rockList
.
Count
);
rockList
[
index
].
SetActive
(
true
);
Destroy
(
gameObject
);
//rockList.Remove(rockList[index]);
}
//过滤掉active状态的巨石
public
void
FilterRock
()
{
for
(
int
i
=
rockList
.
Count
-
1
;
i
>=
0
;
i
--)
{
if
(
rockList
[
i
].
activeSelf
==
true
)
{
rockList
.
RemoveAt
(
i
);
}
}
}
}
StockRunning/Assets/New Terrain 1.asset
0 → 100644
View file @
716504db
File added
StockRunning/Assets/New Terrain 1.asset.meta
0 → 100644
View file @
716504db
fileFormatVersion: 2
guid: a406def67bb47d442b9788a99642b0f4
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:
StockRunning/Assets/New Terrain.asset
0 → 100644
View file @
716504db
File added
StockRunning/Assets/New Terrain.asset.meta
0 → 100644
View file @
716504db
fileFormatVersion: 2
guid: 046ba4abb6d837c4abf65c8bd25c8422
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment