Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yisusgamer55 · May 08, 2019 at 04:18 PM · transformpositionunity 2danimation clipcurves

Create an animation with variables?

I'm trying to make a little platform game, and I've created an enemy that when you enter your range of vision, chase you and jump on top of you to crush you. The part of "Jump and fall on you" I decided to do it by means of an animation, but my problem is that when changing position, the animation is executed in the initial position of the enemy, not in the current position of the enemy, I do not know if I explain. I have read that it can be done using animation curves, but I do not understand how it works ... Here is my code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Enemy2 : MonoBehaviour{

 public bool followPlayer = false;
 public float speedToFollow = 2f;
 private Transform target;
 private GameObject player;
 public float distancia = 1f;
 public SpriteRenderer emote;
 private Animator anim;
 private SpriteRenderer mySprite;

 public float maxSpeed = 1f;
 public float speed = 1f;

 public GameObject playerCollider;

 private Rigidbody2D rb2d;

 public float visionRadius = 2f;
 public float attackRadius;

 void Start(){
     target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
     player = GameObject.FindGameObjectWithTag("Player");
     rb2d = GetComponent<Rigidbody2D>();
     emote.enabled = false;
     anim = GetComponent<Animator>();
     mySprite = GetComponent<SpriteRenderer>();

 }

 // Update is called once per frame
 void Update(){

     if (target == null)
         return;

     Debug.Log(rb2d.velocity.x);
     //Debug.Log(Vector2.Distance(transform.position, target.position));

     if (followPlayer == true && Vector2.Distance(transform.position, target.position) > distancia){
         transform.position = Vector2.MoveTowards(transform.position, target.position, speedToFollow * Time.deltaTime);
         //transform.position += transform.forward * speedToFollow * Time.deltaTime;
     }

     if (followPlayer == true && Vector2.Distance(transform.position, target.position) < distancia) {
         //this is where the enemy would perform the animation of jumping on the player
     }
 }

 void FixedUpdate(){
     if (followPlayer == false) {
         rb2d.AddForce(Vector2.right * speed);
         float limitedSpeed = Mathf.Clamp(rb2d.velocity.x, -maxSpeed, maxSpeed);
         rb2d.velocity = new Vector2(limitedSpeed, rb2d.velocity.y);

         if (rb2d.velocity.x > -0.01f && rb2d.velocity.x < 0.01f){
             speed = -speed;
             rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
         }

         if (speed < 0){
             transform.localScale = new Vector3(1f, 1f, 1f);
         }
         else if (speed > 0){
             transform.localScale = new Vector3(-1f, 1f, 1f);
         }

         //Debug.Log (rb2d.velocity.x);
     }
 }

 private IEnumerator OnTriggerEnter2D(Collider2D collision){
     if (collision.gameObject.tag == "Player") {
         followPlayer = true;
         anim.GetComponent<Animator>().SetBool("Attack", true);
         yield return new WaitForSeconds(0.5f);
         anim.GetComponent<Animator>().SetBool("Attack", false);
         

     }
     if (collision.gameObject.tag == "Vuelta"){
         speed = -speed;
         rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
     }
 }

 private void OnTriggerStay2D(Collider2D collision){
     if (collision.gameObject.tag == "Player"){
         followPlayer = true;
     }
 }

 private void OnTriggerExit2D(Collider2D collision){
     followPlayer = false;
 }

 private void OnDrawGizmos(){
     Gizmos.color = Color.yellow;
     Gizmos.DrawWireSphere(transform.position, visionRadius);
     Gizmos.DrawWireSphere(transform.position, attackRadius);
 }

 private IEnumerator Wait(float seconds) {
     yield return new WaitForSeconds(seconds);
 }

}

alt text

Is there any way to solve this? Any help is very much appreciated.

unity.png (18.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by yisusgamer55 · May 11, 2019 at 07:54 PM

Hello! In the end I found the solution. All he had to do is create a father object with the colliders of the enemy and with his scrit, leaving the animator in the main object of the enemy. By doing this, the enemy's transform varies with respect to the father's position.

I found the solution in this thread: https://answers.unity.com/questions/51178/animation-position-problem.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

152 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding a value to transform.position.y runs very untrusted 0 Answers

scale based on position(help) 1 Answer

Keeping an Object Locked In Position Around Another 1 Answer

why is transform.position giving me local location?(c#) 0 Answers

Floating-point errors while assigning transforms 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges