Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JoshMBeyer · Jan 22, 2013 at 06:37 AM · enemymovetargettowards

Can anyone tell me whats wrong with my MoveTowardsEnemy part of my script?

I am making a simple script to where when the player attacks, he moves toward the enemy. Now I got it to where the player will face the enemy, and move to him. But when the player moves towards the enemy he shrinks? and the closer he gets the slow he moves.. and he just repeatedly runs into the enemy's feet.. and if I try to move I fall through the floor.

Any Ideas on whats wrong here?

This is the script I am using,

 using UnityEngine;
 using System.Collections;
 
 
 public class PlayerAttack : MonoBehaviour {
     
     
     //----------Variables Start----------
     
     public         int         moveSpeed;
     public         int         rotationSpeed;
     
     public         Transform     target;
     private     Transform     myTransform;
     
 
     public         float         smooth             = 5.0F;
     public         float         smoothTime         = 0.3F;    
     
     public         bool         iAmAttacking     = false;
     public         bool         iAmTargeting     = false;
     
     private     Vector3     velocity         = Vector3.zero;
     
     //----------Variables End------------
     
     
     //A quick reference of our transform.
     void Awake()
     {
         myTransform = transform;
     }
     
     
     // Update is called once per frame
     void Update () 
     {
         //If we are targeting something, Draw a line between the player, and the target.
         if(iAmTargeting)
         {
             GameObject go = GameObject.FindGameObjectWithTag("Enemy");
             target = go.transform;
             
             Debug.DrawLine(target.transform.position, myTransform.position, Color.red);
         }
         
         //Since when we are targeting/Fighting, our ThirdPersonController is disabled.
         //If we use any Input, enable our ThirdPersonController.
         if(Input.anyKey)
         {
             GameObject player = GameObject.Find("Player");
             PlayerAttack playerAttack = player.GetComponent<PlayerAttack>();
             playerAttack.iAmTargeting = false;
             playerAttack.iAmAttacking = false;
             
             ThirdPersonController thirdPersonController = player.GetComponent<ThirdPersonController>();
             thirdPersonController.enabled = true;
         }
         
         //If we are atttacking, the played will constantly face the enemy, and move towards them.
         if(iAmAttacking) 
         {
             transform.LookAt(target.transform);
             transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * smooth);
         }
     }
 }
 
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 iwaldrop · Jan 22, 2013 at 07:18 AM

One, because you're Lerping from points a > b, but your value is never 1; hence, your player will never arrive at his destination.

http://en.wikipedia.org/wiki/Lerp_(computing)

And two; it's probably because you're using LookAt...so your player is looking at the enemy's actual location (the ground). You're going to want to maintain your x/z rotation and only rotate about the y axis.

Once you get the horizontal rotation sorted, try experimenting with transform.Translate() to move your character instead of Lerping him around.

Comment
Add comment · Show 2 · 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 JoshMBeyer · Jan 22, 2013 at 03:47 PM 0
Share

How would you recommend going about the horizontal rotation?

avatar image iwaldrop · Jan 22, 2013 at 04:31 PM 0
Share

I don't have time right now to give you a great answer, but you can do something like this (and hopefully someone who does have time will improve upon it)...

 public Transform lookTarget;
 public float moveAmount;
 void TurnAnd$$anonymous$$ove()
 {
     Vector3 angles = transform.eulerAngles;
     transform.LookAt(lookTarget);
     angles.y = transform.eulerAngles.y;
     transform.eulerAngles = angles;
     transform.Translate(Vector3.forward * moveAmount);
 }

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Getting enemy to move towards player untill they collide problem... 2 Answers

rebuild target list when gameobject destroyed 1 Answer

Enemy Death and Killing Script 3 Answers

Creating Flowing Water 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