- Home /
Javascript controlling Mecanim?
Hi everyone,
My other problem was solved, but for some reason, one of my models is going into the ground when following my Player character. He has a collider, properly adjusted and all. I can't figure out the problem.
Here's my script:
var Victim: Transform; //target of robot
var damp=1.0; //the speed at which robot turns
var FireballPrefab:Transform;//the prefab for the projectile
var savedTime=0; //time since shooting
var Tdist:float; // distance between robot and Victim
var moveSpeed=15; //moving speed
function Update()
{
//Initializing the animator component
var Mek: Animator = GetComponent(Animator);
//Variables for Shooting
var seconds: int=Time.time;
var oddeven=(seconds%2); //seconds will make the oddeven true or false
//I have 2 parameters right now: Speed and Shooting
if (Mek.GetBool("Shooting"))
{
Mek.SetFloat("Speed",0);
}
//finding dist
if (Victim)
{
var Tdist = Vector3.Distance(Victim.position, transform.position);
Mek.SetFloat("Tdist",Tdist);
}
//In range of Victim
if(Tdist<100)
{
//looking at Victim
var rotate = Quaternion.LookRotation(Victim.position-transform.position);
transform.rotation=Quaternion.Slerp(transform.rotation,rotate,Time.deltaTime*damp);
}
//moving towards Victim
if (Tdist>20 && Tdist<=50)
{
transform.Translate(Vector3.forward *moveSpeed* Time.deltaTime); //move forward
Mek.SetFloat("Speed",moveSpeed); //setting Speed parameter
Mek.SetBool("Shooting",false); //shoot animation off
}
if (Tdist<=20)
{
Mek.SetFloat("Speed",0); //Stop movement animation
Mek.SetBool("Shooting",true); //Start shooting animation
}
if (oddeven && Mek.GetBool("Shooting"))
{
Mek.SetFloat("Speed",0);
Shoot(seconds); //if oddeven is true and distance is <= than 20, call shoot function
}
}
//Shooting Victim
function Shoot(seconds)
{
if (seconds != savedTime)
{
var Fireball= Instantiate(FireballPrefab,transform.Find("TurretPoint").transform.position,Quaternion.identity);
Fireball.rigidbody.AddForce(transform.forward*5000);
savedTime=seconds;
}
}//End of Shooting function
Answer by Hyperion · Jan 24, 2013 at 10:02 PM
Hi everyone, I have fixed everything. No need for help with this question, but can you please look at my other question regarding Root Motion (the question is the Final Barrier of Mecanim)?
Are you going to tell us how you solved the going into the ground problem?
Thanks
Oh, wow, this is a pretty old question. I have accomplished so much since then. Hmmmm..... Let me see if I can remember how I did that....
I believe there might've been 2 things wrong, but one of the solutions was to increase height of collider to match the position of the feet. If you need something more specific, please ask a more specific question with details and all. Hope this helps.
Your answer
Follow this Question
Related Questions
accessing animation states from animator in javascript 0 Answers
Altered Mecanim parameters reset the current state back to the default state. 0 Answers
How can I use 1 key instead of two for toggling animation ? 2 Answers
Changing animation when moving horizontally without flipping sprite 0 Answers
How do I create a function that accepts EITHER vector3 OR three floats? 1 Answer