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 dragonking300 · Jul 23, 2017 at 06:39 AM · c#newbielerping

The enemy movement script I'm modeling after my player's isn't working.

I don't have the traditional movement script for my player. The way it works in a nutshell is I click on a wall in a 360 radius around the player and they lerp to that position with a W.I.P collision system I implemented(Uses raycast to stop lerping if they "collide" with the wall) I thought it would be simple to remove my enemy's movement script and edit my player's movement script a little bit to have it work for them but it apparently isn't that simple and it won't work in alot of ways.

Problems/insights with the enemy's lerping script

  1. The timer to dash at the player won't reset even if I move the player towards the enemy

  2. The timer in the movement courintine won't move form zero

  3. The enemy won't dash at the player

  4. The enemy is supposed to have it's rotation fixed on the player but when it dashes it locks to lerp towards the position the player was at when the timer to charge up a attack reached 0 but only seems to lock on(I think it's in a perpetual state of trying to lerp towards the player?) UPDATE(I put some Debug logs in my code and I noticed CurrentPointToLerpFromPosition is 0, 0, 0 on my enemy script at all times but on my player script it gets a value after I click on a wall?)

Enemy script

  using UnityEngine;
 using System.Collections;
 
 public class BasicEnemyAi : MonoBehaviour {
     public GameObject Enemy;
     public GameObject Player;
     public float ChargeUpTimer = 2;
     private bool LerpToPosition;
     private bool OneTimeStuff;
     private Vector3 ThePointToLerpTo;
     private static GameOverConditions GameOver;
     public float speed;
     private float TimerStart;
     private GameObject dagger;
     public Animator anim;
     public Vector3 PointToLerpFrom;
     private Vector3 RayPointPreviousPlace;
     public Vector3 CurrentPointToLerpFromPosition;
     private Vector3 DoesThisPoint;
      Vector3 newPosition;
     public float timer;
     public float DistanceInRay;
     void Start ()
     {
         newPosition = transform.position;
         TimerStart = ChargeUpTimer;
         Player = GameObject.FindGameObjectWithTag("Player");
     }
     
     // Update is called once per frame
     void Update ()
     {
         Debug.Log(Vector3.Distance(newPosition, Player.transform.position));
         Vector3 rayDir = Player.transform.position - transform.position;
         Ray ray = new Ray(transform.position, rayDir);
         Debug.DrawRay(transform.position, rayDir);
         PointToLerpFrom = ray.origin + (ray.direction * DistanceInRay);
         if (Vector3.Distance(Player.transform.position, Enemy.transform.position) <= 8)
         {
             anim.SetTrigger("InProximity");
         }
         if (LerpToPosition == false)
         {
             if (Player != null)               
                 Enemy.transform.LookAt(Player.transform);
             //    Enemy.transform.eulerAngles = Enemy.transform.eulerAngles + new Vector3(180, 180, );
             ChargeUpTimer -= Time.deltaTime;
         }
         if (ChargeUpTimer <= 0)
         {
             LerpToPosition = true;
             if (LerpToPosition == true)
             {
                 if (Player != null)
                 {
                     if (OneTimeStuff == true)
                     {
                         newPosition = Player.transform.position;
                         OneTimeStuff = false;
                     }
                     TimerCheck();
                 }
                 if(Vector3.Distance(newPosition, Player.transform.position) <= 3)
                 {
                     LerpToPosition = false;
                     OneTimeStuff = true;
                     ChargeUpTimer = TimerStart;
                 }
             }
             else
             {
                 LerpToPosition = false;
             }
         }
     }
     private void TimerCheck()
     {
         if(ChargeUpTimer <= 0)
         {
 
             StartCoroutine("LerpPos");
         }
     }
     private IEnumerator LerpPos()
     {
        
         if (DoesThisPoint != Player.transform.position)
         {
             timer = 0;
         }
         //DoesThisPoint = NewPosition;
         while (Vector3.Distance(CurrentPointToLerpFromPosition, newPosition) > 3)
         {
             Debug.Log("My gawd");
             timer += Time.deltaTime;
             RayPointPreviousPlace = CurrentPointToLerpFromPosition;
             CurrentPointToLerpFromPosition = Vector3.Lerp(PointToLerpFrom, newPosition, timer * speed);
             transform.position = RayPointPreviousPlace;
             yield return null;
         }
     }
 }

Player script

 using UnityEngine;
 using System.Collections;
 
 public class Dash : MonoBehaviour
 {
     public GameObject Previouscollided; // the point last lerped from
     private GameObject collided; //A check to see if the camera clicked on the wall
     public LayerMask Wall; //Self documenting but its basicly the wall that you click on
     Vector3 newPosition; //The Position the player assumes once lerping is done
     public GameObject RecentWall; //The wall that was clicked on before Current Wall
     public float speed;//How fast lerping happens
     [SerializeField]
     public int CurrentWall; //The last wall that was clicked on
     public float DistanceInRay; //The length to in the camera's array to set PointToLerpFrom
     private Vector3 PointToLerpFrom; //The point after all calculations that the object lerps from
     private Vector3 RayPointPreviousPlace;
     private Vector3 CurrentPointToLerpFromPosition;
     private Vector3 DoesThisPoint;
     public float timer;
     void Start()
     {
         newPosition = transform.position;
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit, Wall.value))
             {
                 PointToLerpFrom = ray.origin + (ray.direction * DistanceInRay);
 
                 collided = hit.collider.gameObject;
                 RecentWall = collided;
                 newPosition = hit.point;
 
                 Previouscollided.layer = LayerMask.NameToLayer("Wall");
                 Previouscollided = hit.collider.gameObject;
                 CollidedCheck();
             }
         }
     }
     private void CollidedCheck()
     {
         if (collided.tag == "Wall")
         {
             StartCoroutine("LerpPos");
         }
     }
 
     private IEnumerator LerpPos()
     {
         if (DoesThisPoint != newPosition)
         {
             timer = 0;
         }
         //DoesThisPoint = NewPosition;
         while (Vector3.Distance(CurrentPointToLerpFromPosition, newPosition) > 3)
         {         
             timer += Time.deltaTime;
             RayPointPreviousPlace = CurrentPointToLerpFromPosition;
             CurrentPointToLerpFromPosition = Vector3.Lerp(PointToLerpFrom, newPosition, timer * speed);
             transform.position = RayPointPreviousPlace;
             yield return null;
         } 
     }
 }

I wanted to trying to stop relying on unity answers and have been working on this for a lot of hours but I don't know what I'm doing wrong :/

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

Answer by tanoshimi · Jul 23, 2017 at 07:09 AM

In the process of transferring your player script to work for your enemies, you don't seem to have updated many of the important references.

 Vector3 rayDir = Player.transform.position - transform.position;

for example - why is the enemy casting a ray from the player's position?

Comment
Add comment · Show 1 · 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
avatar image dragonking300 · Jul 23, 2017 at 07:20 AM 0
Share

actually no you're wrong. Its being cast from the enemy's position as when I switch them the ray is pointing backwards from the player

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

355 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 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 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

My object now lerps but it only lerps once 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

External Libraries in C# scripts 1 Answer

Why my object not disappear? 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