- Home /
This doesnt work,help?
I want a animation to be played if my player has walked X amount of units. the player can only walk in a strait line, so I just have to check that line. the player cant walk backwards.
this is my script, I don't see whats wrong...
using UnityEngine; using System.Collections;
public class carAnimationScript : MonoBehaviour {
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(gameObject.transform.position.z == 14)
{
animation.Play("car animation");
}
}
}
Answer by HypoXic5665 · Jul 18, 2013 at 07:58 PM
The problem probably comes from your if condition. Currently you have your if condition is only true if the position.z is EXACTLY = to 14. It is unlikly that a float like this will equal exactly the number you are shooting for.
Try changing
if(gameObject.transform.position.z == 14)
to
if(gameObject.transform.position.z >= 14)
And see if that points you in a better direction.
So why would this be? Your characters pos.z must be greater than 14 when you start the game.
If you want the animation to play for every 14 units traveled you are going to have to code a counter that adds the delta distance from the last frame every frame. From this you can have a condition in the statement that will check to see if that number is greater than 14. If so, it should play the animation and reset the counter.
Try to code this up for yourself and post it here so we can help you out.
oh I see y. I have the script on the car, so it checks that... but when I set it on my player he just start doing that animation over and over again.
I have to get in to my player from my car.. can I do that?
You can do anything, with science! But I do not understand. What exactly are you trying to achieve?
I want to enter my players position from my script on the car. like make one of those thins where you can drag and drop a gameoject to and then take the transform from the choosen object.
Your answer
Follow this Question
Related Questions
Dragg 2D object? 1 Answer
When i double Tap single Tap function also Executing.. 1 Answer
Adding animation from other model on character 0 Answers
Best way to store large quantities of information 2 Answers
Quaternion.LookRotation over time? 0 Answers