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 /
  • Help Room /
avatar image
0
Question by Tageos · Feb 13, 2016 at 11:14 PM · c#movementvector3enemytransform.position

Move a object along a vector

Hello!

I am trying to give the enemy movement in the direction of the player using this:

 transform.position += playerPos.normalized * 10 * Time.deltaTime;

Its not working quite well though, the enemy moves in reverse y-direction or something.alt text This is the rest of the code:

 using UnityEngine;
 using System.Collections;
 
 public class ninjaScript : MonoBehaviour {
 
     //Ninja spawn phase
     private Vector3 ninjaSpawn;
     private bool hasSpawned = true;
        public bool slidDone;
     public bool hasSlid;
     public int dice;
     public float slideSpeed;
     
     //Ninja attack phase
     public bool attackDone;
     public bool hasAttacked;
     public Vector3 playerPos;
     private Vector3 targetPos;
 
     
     void Start () {
         dice = Random.Range (-3, 3);
         hasSpawned = true;
     }
 
     void Update () {
         if (!hasSlid){
         slide (); 
         }
         if (hasSlid){
         attackCycle ();
         }
     }
 
     void slide(){
         transform.Translate(0,-slideSpeed*Time.deltaTime,0);
         if(this.gameObject.transform.position.y < dice){
             slidDone = true;
         }
 
         if (slidDone){
             hasSlid = true;
         }
     }
 
     void attackCycle (){
         if (!hasAttacked){
         attack ();
         }
         if (attackDone){
         hasAttacked = false;
         }
     }
 
     void attack () {
         playerPos = GameObject.FindGameObjectWithTag ("Player").transform.position;
         transform.position += playerPos.normalized * 10 * Time.deltaTime;
     }
 }

If someone has any tips id really appretiate it!

untitled2.png (3.9 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
1
Best Answer

Answer by Eno-Khaon · Feb 13, 2016 at 11:26 PM

Your means of moving towards the player's position is flawed.

What you're adding is the player's absolute position.

What you want is to add the player's relative position. The direction from vector A to vector B is calculated as B - A. Therefore, it's a fairly simple change you're needing to make:

 transform.position += (playerPos - transform.position).normalized * 10 * Time.deltaTime;
Comment
Add comment · Show 6 · 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 Tageos · Feb 13, 2016 at 11:41 PM 0
Share

oh ofcourse, thank you very much!

avatar image Tageos · Feb 13, 2016 at 11:47 PM 0
Share

I forgot to mention that i want to make the enemy keep moving if he misses the player. This solution makes him stop once reaching the player. Do you think i have to rethink the solution or can i just modify transform.position += (playerPos - transform.position).normalized * 10 * Time.deltaTime; ?

avatar image TreyH Tageos · Feb 14, 2016 at 12:10 AM 0
Share

"misses the player" is unlikely, as you're calling this every frame. Any overshooting will be accounted for on the very next frame with a new direction, sending your object back towards its target.

If you want your object capable of "missing" its target, a few options are:

1- Poll the target's location at some time and move towards that fixed location. 2- Have your object move in a fixed direction and adjust its direction by a certain amount each update in order to eventually face your target.

avatar image Tageos TreyH · Feb 14, 2016 at 12:28 AM 0
Share

Thank you for the tips, i have been investigating option #1 but i cant really find a good method without using rigidbody. Because the enemy eventually stops (when hitting a wall) i thought of using the vector3.movetoward with a target point on the wall. It got real messy when i tried to pinpoint the point on the wall (using the equation for a line y = ax+ b and the player position). there has to be a way to call a function once that tells the enemy to move along a vector..

avatar image Eno-Khaon Tageos · Feb 14, 2016 at 12:45 AM 0
Share

You can create a variable for the direction, set it to (playerPos - transform.position).normalized, then just use that variable to move in a fixed direction at a time. As an example of this:

 public float resetTime = 3.0f;
 float currentTime = 0.0f;
 Vector3 moveDirection;
 
 void Update()
 {
     currentTime += Time.deltaTime;
     if(currentTime >= resetTime)
     {
         currentTime = 0.0f;
         moveDirection = (playerPos - transform.position).normalized;
     }
 }
avatar image Tageos Eno-Khaon · Feb 14, 2016 at 01:38 AM 0
Share

Thanks you!

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

91 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

Related Questions

Movement Sticking 0 Answers

How to make more realistic bot AI? 2 Answers

C# AI move on Sight 1 Answer

Reversing movement directions 1 Answer

transform.position is not updating the object's position 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