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 roncel106 · Aug 18, 2012 at 06:09 PM · android2dphysicsrigidbodyai

How do I make this kind of follow script?

So basically, I'm making a 2d android game similar to the game asteroids but instead of asteroids, I fight alien spaceships. What I want my alien spaceship to do is look at the player and follow it. When the alien spaceship reaches a certain distance,like for example the ship is 5 units near my ship, it stops following my ship but continues to look at it, then if i move closer to the alien spaceship, it moves backwards as if it was avoiding collision to my ship.

Then here's the catch, my ship has the ability to push the aliens away using this line of code:


            var explosionPos : Vector3 = transform.position;
	    	var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
	    
	    		for (var hit : Collider in colliders) {
	        		if (!hit)
	            		continue;
	        
	        		if (hit.rigidbody)
	            		hit.rigidbody.AddExplosionForce(power, explosionPos, radius);
				}

In other words, I made the alien spaceships rigidbodies.

How do I make a code in which that when I apply an explosion force to them, they temporarily get pushed back, then after getting pushed away for a while, the alien looks at and follows the player once again?

I hope that those who read this is getting me. I would really appreciate the help you would give me since this code you're about to teach me plays an important role in my game.

Thanks a lot! :D

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 Azuri · Aug 19, 2012 at 02:30 AM

put this script on the alien spaceship

 var playerSpaceShip:Transform; // assign the player spaceship here
 function Update() {
     // this rotates the alienspaceship into the player direction
     var wantedRotation : Quaternion = Quaternion.LookRotation(transform.position - playerSpaceShip.position, Vector3.forward);
     wantedRotation.x = 0.0;
     wantedRotation.y = 0.0;
     playerSpaceShip.rotation = wantedRotation;
     
     //move forward
     if ( Vector3.Distance(transform.position, playerSpaceShip.position) < 5 ) // if distance < 5 units then stop moving forward
         transform.Translate(Vector3.up * 1 * Time.deltaTime);// this moves it forward
 }
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 roncel106 · Aug 19, 2012 at 07:23 AM 0
Share

it's kinda getting screwed up, since I rotated my model 270 degrees.

and the movement gets ruined when the explosion force is applied

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a solution to when colliders bypass? 2 Answers

[Complex] How to move the player similar to character controller while also applying rigidbody physics vectors 1 Answer

Best way to move a rigidbody 1 Answer

Unity 4.3 CharacterController collision with physics 2D not working 1 Answer

AddForce vs Velocity issues with Rigidbody2D 2 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