Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unity สคริปต์ Unity นี้มีความผิดพลาดตรงไหนครับ
#1
Question 
สคริปต์ Unity นี้มีความผิดพลาดตรงไหนครับ
----------------------------------------------------------------------
var PlayerState : float;

function Update ()
{
PlayerStateController();
PlayerAnims();


}

function PlayerStateController()
{
if (Input.GetAxis("Vertical")!=0 || Input.GetAxis("Horizontal") !=0)
{
if (Input.GetButton("Sprint"))
{
PlayerState = 2;
}
}
else
{
PlayerState = 0;
}

}
function PlayerAnims()
{
if (PlayerState == 0)
{
PlayerAnimSec.animation.CrossFade("idle animation",0.4);
}
else if (PlayerState == 1)
{
PlayerAnimSec.animation.CrossFade("walk animation",0.4);
}
else if (PlayerState == 2)
{
PlayerAnimSec.animation.CrossFade("sprint animation",0.4);
}
}

----------------------------------------------------------------------


ตัวโปรแกรมผมมันแจ้งว่าไม่รู้จัก PlayerAnimSec ตลอดเลยไม่ทราบว่าผิดพลาดตรงไหนหรอครับ สคริปต์นี้ผมได้มาจากคลิปของฝรั่ง แต่เค้าก็ไม่เห็นผิดพลาดอะไร รบกวนผู้รู้ด้วยนะครับ
ขอบคุณครับ Confused
[-] The following 1 user says Thank You to lightwolfz for this post:
  • นิราจ
Reply
#2
อยากช่วยมากเลยนะครับเเต่ผมไม่มีความรุเรื่องโปรเเกรมนี้เลยเเม้เเต่ขี้เล็บ =.=
[Image: SOHucL]
[-] The following 1 user says Thank You to pomlove for this post:
  • lightwolfz
Reply
#3
ลองเปลี่ยน PlayerState เป็น int ดูครับ แต่ตามหลักน่าจะถูกแล้วน่ะ
====================ให้เติมไว้ด้านบน=================

var PlayerState : int;
[Image: btpni.jpg]
[-] The following 1 user says Thank You to นิราจ for this post:
  • lightwolfz
Reply
#4
๛คุณลืมประกาศตัวแปร PlayerAnimSec นี่ครับ

เพิ่ม

var PlayerAnimSec : GameObject;

ไว้บนสุดก็หาย error เอง แล้วอย่าลืม Assign โมเดลเข้าไปใน PlayerAnimSec ด้วยนะครับ
[-] The following 3 users say Thank You to Griever for this post:
  • Author, lightwolfz, Nayru
Reply
#5
เพิ่มเติมจากท่าน Griever

ถ้าเกิด error ประเภทที่ว่าไม่รู้จักตัวนู้นตัวนี้ ให้ลองดูว่าตัวที่มันไม่รู้จักนั้น ได้มีการประกาศไว้แล้วหรือยัง จะเอามาใช้เฉยๆเลยไม่ได้ครับ

(ยกตัวอย่าง หากคุณเอาบรรทัด var playerState ออก มันก็จะแจ้งว่าำไม่รู้จัก playerState เหมือนกันคับ)
[Image: Signature-Alex.png]
[-] The following 2 users say Thank You to Nayru for this post:
  • Author, lightwolfz
Reply
#6
มาไม่ทัน คนอื่นตอบไปหมดแล้ว T T

แก้ไขให้อีกนิดแล้วกันครับ Playerstate เห็นที่ใช้แทน State ตัวละคร แทนด้วยเลข 1,2,3,4 ใช้เป็น int ก็ได้ครับ

float ใช้กับพวกเลขทศนิยม

อีกอย่างถ้าจะทำ State ตัวละคร แนะนำใช้ enum ครับ สะดวกกว่าเยอะเลย

ปล. มีคนใช้ยูนิตี้เพิ่มอีกคนแหะ
[-] The following 1 user says Thank You to Author for this post:
  • lightwolfz
Reply
#7
ได้แล้วครับ ขอบคุณทุกคนมากๆนะครับ

ผมมีปัญหาอีกครับคือพอสคริปต์สมบูรณ์ใช้ได้ละ พอลองรันดูก็มีปัญหาคือ พอเราอยู่ PlayerState 0 ผมปรับเป็น vertical แต่มันโชว์ animation ของ playerstate 1 ไม่ใช่ playerstate 0 ผมมางง ตรงนี้หล่ะครับ ขอความกรุณาอีกหน่อยเด้อ
Reply
#8
ดูจากโค้ดไม่เห็นมีอันใหนที่ปรับ

Playerstate ให้ = 1 เลยนะครับ

จากโค้ด

if (Input.GetButton("Sprint"))
{
PlayerState = 2;
}
}
else
{
PlayerState = 0;
}

ถ้า PlayerState จะเปลี่ยนก็มีแค่ 0,2 นี่ครับ
[-] The following 1 user says Thank You to Author for this post:
  • lightwolfz
Reply
#9
(09-16-2013, 08:09 PM)Author Wrote: ดูจากโค้ดไม่เห็นมีอันใหนที่ปรับ

Playerstate ให้ = 1 เลยนะครับ

จากโค้ด

if (Input.GetButton("Sprint"))
{
PlayerState = 2;
}
}
else
{
PlayerState = 0;
}

ถ้า PlayerState จะเปลี่ยนก็มีแค่ 0,2 นี่ครับ

ควรแก้ไขยังไงครับ ไปไม่เป็นแฮะ Forever Alone
Reply
#10
Quote:ควรแก้ไขยังไงครับ ไปไม่เป็นแฮะ Forever Alone

จากที่อ่าน code เดาว่า น่าจะเป็น code ควบคุม state ตัวละคร
ซึ่งมี 3 สถานะคือ
1. ยืนนิ่งๆ (Idle)
2. เดิน (Walk)
3. วิ่ง (Sprint)

ดังนั้น
มันขาด State ที่เปลี่ยน PlayerState = 1 ครับ
ดังนั้นต้อง เพิ่มเข้าไป ดังนี้
Code:
function PlayerStateController()
{
     if (Input.GetAxis("Vertical")!=0 || Input.GetAxis("Horizontal") !=0)
     {
          if (Input.GetButton("Sprint"))
          {
               PlayerState = 2;
          }
          else if( เงื่อนไขการ walk )
          {
               PlayerState = 1;
          }
     }
     else
     {
          PlayerState = 0;
     }
}

ส่วนเงื่อนไขการเดิน ผมขอเดาว่า น่าจะเป็นกดปุ่ม AWSD หรือเปล่า
ถ้าเป็นแบบนี้ ให้ใส่
Code:
Input.GetKeyDown(Keycode.A) || Input.GetKeyDown(Keycode.W) Input.GetKeyDown(Keycode.S) || Input.GetKeyDown(Keycode.D)
เข้าไปในวงเล็บครับ
[-] The following 1 user says Thank You to Cliff for this post:
  • lightwolfz
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)