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
1
Question by Henrique_Smallio_ · Sep 09, 2015 at 03:14 PM · c#physicsprogramming

Making a game object follow the player C#

So first of all let me apologize if this is a stupidly simple question, I have only touched unity today. So I am trying to make a 2D game and I am having problems making an enemy sprite follow the player sprite. I have already made a script, that to me makes perfect sense, But doesn't work:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyMovement : MonoBehaviour {
 
 
     [SerializeField]
     private GameObject player;
     private Rigidbody2D body;
     public Vector2 velocity = Vector2.zero;
 
     // Use this for initialization
     void Start () {
         body = GetComponent <Rigidbody2D> ();
     }
     
     // Update is called once per frame
     void Update () {
         if (transform.position.x > body.transform.position.x){
             body.velocity = velocity * -1;
         } else if (transform.position.x < body.transform.position.x){
             body.velocity = velocity;
         } else {
             body.velocity = new Vector2 (0,0);
         }
     }
 }

I have set the velocity and the player game object yet the enemy sprite just sits there. If anyone could help that would be amazing. I just don't want to find the fix but also know why this didn't work.

Thank you.

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
3
Best Answer

Answer by MartinLyne · Sep 09, 2015 at 03:48 PM

I can't see where you actually use the player's position? Perhaps you meant if (player.transform.position.x > body.transform.position.x) etc?

Also, when you deal with Ridigbody components you should make changes to it in FixedUpdate not the normal Update method (see http://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html)

Lastly, you shouldn't change the velocity value directly (it doesn't like it) see: http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

Don't worry, they are common pitfalls!

Try something like this:

  void FixedUpdate () {
         Vector2 toTarget = player.transform.position - transform.position;
         float speed = 1.5f;
         
         transform.Translate(toTarget * speed * Time.deltaTime);
     }

Disclaimer: I'm usualy in 3D not 2D so if anything acts a little strange it might be because of that - just add a comment and mention my name I'll do what I can. Hope that helps!

Comment
Add comment · Show 4 · 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 Henrique_Smallio_ · Sep 09, 2015 at 06:33 PM 0
Share

Ah I wasn't meant to be using the body's position. oops.Silly mistake that I completely over looked. I changed it to player and it all works now

  if (transform.position.x > player.transform.position.x){

I also put it in void Fixed update and set the velocity value in code. Thanks for all the help man, you helped out a alot. :)

avatar image MartinLyne Henrique_Smallio_ · Sep 10, 2015 at 08:09 AM 0
Share

No problem, have fun!

avatar image aadarsh-ram MartinLyne · Aug 13, 2016 at 05:35 AM 0
Share

Hi @$$anonymous$$artinLyne,

I used @Henrique_Smallio_ 2d Eneny$$anonymous$$ovement script. I made the if (transform.position.x > player.transform.position.x){ change. The script makes the enemy chase strangely. I am making a Electric Circuit inspired game. Pls tell me what to do. I'll attach the pic and script.

Script: using UnityEngine; using System.Collections;

public class NewChaser2d : $$anonymous$$onoBehaviour {

 [SerializeField]
 private GameObject player;
 private Rigidbody2D body;
 public Vector2 velocity;

 // Use this for initialization
 void Start()
 {
     body = GetComponent<Rigidbody2D>();
 }

 // Update is called once per frame
 void FixedUpdate()
 { 
     if (transform.position.x > player.transform.position.x)
     {
         body.velocity = velocity * -1;
     }
     else if (transform.position.x < body.transform.position.x)
     {
         body.velocity = velocity;
     }
     else {
         body.velocity = new Vector2(0, 0);
     }
 }

}

alt text

The ghost is the enemy and the electron is the target.

game1.png (84.7 kB)
Show more comments

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

30 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

Related Questions

What is the correct way to learn unity scripting ? 2 Answers

I need help with Sonic style slope physics 0 Answers

Collision Detection with DrawMeshInstanced 0 Answers

Real aerodynamics moving surfaces 0 Answers

Bounce between two exact points 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