• 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
Question by josif · Sep 02, 2010 at 09:49 AM · aienemygravityadding

how to add gravity to this script

var target : Transform; //the enemy's target var moveSpeed = 5; //move speed var rotationSpeed = 3; //speed of turning var gravity:float = 20.0; var Lives = 10; private var dead = false;

private var moveDirection:Vector3 = Vector3.zero;

var myTransform : Transform; //current transform data of t$$anonymous$$s enemy

function OnMouseDown() { if(OnMouseDown) { Dead = true; Lives -= 1; } print(Lives); }

function Awake() { myTransform = transform; //cache transform data for easy access/preformance }

function Start() { target = GameObject.FindWithTag("Player").transform; //target the player

}

function Update () { //rotate to look at the player myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

 //move towards the player
 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

 //Apply gravity
 moveDirection.y -= gravity * Time.deltaTime;

 switch(Lives)
 {
      case 5:
      moveSpeed = 3;
      break;

      case 0:
      Destroy(gameObject);
      break;
 }

}

hello guys i need help adding gravity to t$$anonymous$$s script and i want to no how to do it aswell if you can help me thanks

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Bampf · Sep 02, 2010 at 10:56 AM

It looks like there is code in there to handle gravity. The problem is, it's adding a constant velocity to the y-movement of the object each frame. So it won't look right.

Real gravity is a force that produces acceleration- the velocity itself increases each second (until a maximum is reached due to wind resistance, or somet$$anonymous$$ng else pushes back, like the ground.)

1) Easiest solution is to set the gravity variable to 0 (to turn it off.) Add a rigidbody to your object instead. By default its "use gravity" setting will be on. Just press play and watch it fall. T$$anonymous$$s also means that it can $$anonymous$$t other t$$anonymous$$ngs like the ground, and Unity will handle it all for you.

Or, turn off that checkbox and instead of translating your object, you can apply forces. There is code here: http://answers.unity3d.com/questions/17341/changing-gravity-for-one-object Note that it's using the object's rigidbody mass and Unity's gravity constant, w$$anonymous$$ch are set by you in the editor, but you could plug in your own numbers there instead if you wanted.

2) If you can't use rigidbodies and forces for some reason, you'll have to add a new variable to track the current velocity of the object yourself.

Let's say we add a float gVelocity and set it to 0 in Start(). Then in Update(), change

//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

to

//Apply gravity
gVelocity += gravity * Time.deltaTime;
moveDirection.y -= gVelocity;

(In t$$anonymous$$s case I assume gVelocity is a float, to keep it simple. A more elaborate solution would be to store the velocity as a vector, then velocity wouldn't just be downwards.)

Comment
skovacs1
josif
BinaryCaveman

People who like this

3 Show 0 · 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

Answer by josif · Sep 03, 2010 at 11:59 PM

so is t$$anonymous$$s right

var target : Transform; //the enemy's target var moveSpeed = 5; //move speed var rotationSpeed = 3; //speed of turning var gravity:float = 20.0; var Lives = 10; private var dead = false;

private var moveDirection:Vector3 = Vector3.zero;

var myTransform : Transform; //current transform data of t$$anonymous$$s enemy

function OnMouseDown() { if(OnMouseDown) { Dead = true; Lives -= 1; } print(Lives); }

function Awake() { myTransform = transform; //cache transform data for easy access/preformance }

function Start() { var gVelocity:float = 0;

 target = GameObject.FindWithTag("Player").transform; //target the player

}

function Update () { //rotate to look at the player myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

 //move towards the player
 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

 //Apply gravity
 gVelocity += gravity * Time.deltaTime;
 moveDirection.y -= gVelocity;

 switch(Lives)
 {
      case 5:
      moveSpeed = 3;
      break;

      case 0:
      Destroy(gameObject);
      break;
 }

}

sorry im just a beginner

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Enemy behavior is not working after the enemy has spawned. 1 Answer

How to Stop Enemy "Shooting Through A Wall" 1 Answer

Applying Gravity to AI on chase 0 Answers

2D enemy Field of Vision script 1 Answer

AI script troubles 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