- Home /
Question by
rob444 · May 23, 2015 at 02:25 PM ·
script error
BCE0048: Type 'UnityEngine.Component' does not support slicing.
Im getting this error with this part of the code
if(hasAnimation) animation["walk"].speed = 2;
if(hasAnimation) animation["run"].speed = 3;
Here is the full code
var mainCamera : GameObject;
var mainCameraScript;
var hasAnimation : boolean;
var isMoving : boolean;
private var walkSpeed : float = 1.0;
private var gravity = 100.0;
private var moveDirection : Vector3 = Vector3.zero;
private var charController : CharacterController;
///////////////////////////////////////////////////////////////
// FUNCTION :: Start ////////////////////////////////////////
///////////////////////////////////////////////////////////////
function Start(){
charController = GetComponent(CharacterController);
if(hasAnimation) animation.wrapMode = WrapMode.Loop;
mainCameraScript = mainCamera.GetComponent("controller_camera");
print (mainCamera);
print (mainCameraScript);
// set animation clip speeds
if(hasAnimation) animation["walk"].speed = 2;
if(hasAnimation) animation["run"].speed = 3;
// if(hasAnimation) animation["attack"].speed = 1;
}
////////////////////////////////////////////////////////
// FUNCTION :: Update ////////////////////////////////
////////////////////////////////////////////////////////
function Update(){
// make sure the character is in contact with the ground
if(charController.isGrounded == true){
/////////////////////////////////////
// set which animation clip is playing //
/////////////////////////////////////
if(Input.GetButton("Escape")){
Application.Quit();
}
// if the player is moving forwards...
if(Input.GetAxis("Vertical") > .1 || Input.GetAxis("Vertical") < -.1 || Input.GetAxis("Horizontal") > .1 || Input.GetAxis("Horizontal") < -.1){
// run or walk?
if(Input.GetButton("Run"))
{
walkSpeed = 6.8;
if(hasAnimation){ animation.CrossFade("run"); }
} else {
walkSpeed = 1.8;
if(hasAnimation){ animation.CrossFade("walk"); }
}
}
// if no movement, fade to idle
else
{
if(hasAnimation){ animation.CrossFade("idle"); }
}
/*if(Input.GetButton("Attack"))
if(hasAnimation){ animation.Play("attack"); }
else
if(hasAnimation){ animation.CrossFade("idle"); }
*/
////////////////////////////////////////
// prepare the players movement vector //
////////////////////////////////////////
// rotate the player based on horizontal movement requests
var playerY = transform.eulerAngles.y;
var cameraY = mainCamera.transform.eulerAngles.y;
// default
var playerRotY = cameraY;
var playerMovX = 0;
var playerMovZ = 0;
// move north (relative to camera)
if(Input.GetAxis("Vertical") > .1 && Input.GetAxis("Horizontal") > -.1 && Input.GetAxis("Horizontal") < .1){
playerRotY = cameraY; // facing direction
playerMovX = 0;
playerMovZ = 1;
}
// move north east (relative to camera)
if(Input.GetAxis("Vertical") > .1 && Input.GetAxis("Horizontal") > .1){
playerRotY = cameraY + 45; // facing direction
playerMovX = 0;
playerMovZ = 1;
}
// move east (relative to camera)
if(Input.GetAxis("Horizontal") > .1 && Input.GetAxis("Vertical") > -.1 && Input.GetAxis("Vertical") < .1){
playerRotY = cameraY + 90; // facing direction
playerMovX = 0;
playerMovZ = 1;
}
// move south east (relative to camera)
if(Input.GetAxis("Horizontal") > .1 && Input.GetAxis("Vertical") < -.1){
playerRotY = cameraY + 135; // facing direction
playerMovX = 0;
playerMovZ = 1;
}
// move south (relative to camera)
if(Input.GetAxis("Vertical") < -.1 && Input.GetAxis("Horizontal") > -.1 && Input.GetAxis("Horizontal") < .1){
playerRotY = cameraY - 180; // facing direction
playerMovX = 0;
playerMovZ = 1;
}
// move south west (relative to camera)
if(Input.GetAxis("Vertical") < -.1 && Input.GetAxis("Horizontal") < -.1){
playerRotY = cameraY + 225; // facing direction
playerMovX = 0;
playerMovZ = 1;
}
// move west (relative to camera)
if(Input.GetAxis("Horizontal") < -.1 && Input.GetAxis("Vertical") > -.1 && Input.GetAxis("Vertical") < .1){
playerRotY = cameraY - 90; // facing direction
playerMovX = 0;
playerMovZ = 1;
}
// move north west (relative to camera)
if(Input.GetAxis("Vertical") > .1 && Input.GetAxis("Horizontal") < -.1){
playerRotY = cameraY - 45; // facing direction
playerMovX = 0;
playerMovZ = 1;
}
// if the character is moving, turn them to the direction they are moving in (relative to the camera)
if(isMoving == true){
var rotationDelay = Quaternion.Slerp(Quaternion.Euler(0, playerY, 0), Quaternion.Euler(0, playerRotY, 0), Time.deltaTime * 4);
transform.rotation = rotationDelay;
}
// create a movement *unit* vector (relative to the players facing direction)
moveDirection = Vector3(playerMovX,0,playerMovZ);
// transform the direction vector from local space to world space
moveDirection = transform.TransformDirection(moveDirection);
// check if the character has been set into motion
if(moveDirection == Vector3.zero){
isMoving = false;
} else {
isMoving = true;
}
}
/////////////////////////
// transform the player //
/////////////////////////
// push the player down with gravity (this is blocked by terrain collision though)
moveDirection.y -= gravity * Time.deltaTime;
// transform the character with the movement vector
charController.Move(moveDirection * (Time.deltaTime * walkSpeed));
}
////////////////////////////////////////////////////////////////////////////
// FUNCTION :: ClampAngle ////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
static function ClampAngle (angle : float, min : float, max : float)
{
if (angle < -360)
{
angle += 360;
}
if (angle > 360)
{
angle -= 360;
}
return Mathf.Clamp (angle, min, max);
}
Comment