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 hemingso · Mar 02, 2016 at 06:35 AM · c#

How to interract between two different GameObjects

I'm trying to make enemy ships move towards the player ship in a space shooter game while also moving from the top of the screen to the bottom of the screen and gets destroyed by a boundary which functions as it should. And I almost got how I should do it, but I can't figure out the last details.

The relevant script code is:

public class Enemy_Controller : MonoBehavior {

 private float horizontalmovement;
 public float speed4;
 public float speed1;

 void FixedUpdate() {
     horizontalmovement = EnemyShipGameObject.GetComponent<Rigidbody>().position.x -PlayerShipGameObject.GetComponent<Rigidbody>.position.x) { //this line is not correct.
     if (horizontalmovement > 0) {
         GetComponent<Rigidbody>().velocity = (-transform.forward*speed4*time.deltatime +transform.right*speed1*horizontalmovement*time.deltatime)*Quaternion.identity;
     } else {
         GetComponent<Rigidbody>().velocity = (-transform.forward*speed4*time.deltatime +transform.right*speed1*horizontalmovement*time.deltatime)*Quaternion.identity;
     }
 }

}

I'm sorry if the code is a little bit incorrect I have not got it to work in the relevant part of the program and I have also not got any relevant information from searching Quaternion.identity in documentation, but if I remember right it prevents the GameObjects from rotating. In the line marked "this line is not correct". I want to make a connection between the GameObject the script is attached to with tag "EnemyShip" and the GameObject the script is not attached to with tag "Player" and use this information to get the enemy ship to move left if the player ship is currently to the left or move right if the player ship is currently to the right and keep its position if the enemy ship and the player ship is on a line, while also moving down the screen. At the same time as the player ship is constantly in motion.

I think it is only one line (the line marked "this line is not correct") which need to be changed to one long line, and I will be very grateful if someone could tell me how this line best could be written and eventually if there are other small errors in the code. 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
0

Answer by hemingso · Mar 02, 2016 at 02:12 PM

This is another try, but still the position of the player ship is not updated even though the part of code is written in FixedUpdate() can someone please tell med what changes the code needs to make the position of the player ship being updated every frame while the player ship moves instead of just moving to the player ship starting location. And in this case freezing the axes to prevent rotation seems to be a better solution than using Quaternion.identity.

public float speedVertical;

public float speedHorizontal;

private void FixedUpdate() {

     if (target.transform.position.x > this.transform.position.x)
     {
         GetComponent<Rigidbody>().velocity = transform.forward * speedVertical + transform.right * speedHorizontal;
     }
     else if (target.transform.position.x < this.transform.position.x)
     {
         GetComponent<Rigidbody>().velocity = transform.forward * speedVertical - transform.right * speedHorizontal;
     }
     else {
         GetComponent<Rigidbody>().velocity = transform.forward * speedVertical;
     }
 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnTriggerStay2D when trigger is spawned on object not moving 1 Answer

Destroy specific object with tag 2 Answers

Instantiate Previous Prefab in Array 0 Answers

I guys I have a really hard question 0 Answers

if a value of a variable is set on the server using [Command] for the local player is the value set on the local player locally? 0 Answers


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