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
1
Question by bob 1 · Aug 26, 2010 at 10:32 PM · enemyfalling-through-terrainsinking

enemy falls through terrain - code attached

here is the enemy chase that im using in a game. for some reason when the enemy player moves towards my hero he sinks into the ground i think its because he "gravitates" towards the players feet (the xyz position of said feet) and it causes him to sink.... any suggestions ?

using UnityEngine; using System.Collections;

public class EnemyAI : MonoBehaviour { public Transform target; // transform = the x,y,z, coordinates public int movespeed =1; public int rotationspeed =1 ; private Transform mytransform; public int attackdistance = 15; public Vector3 aggroradius;

 void Awake ()
 {
     mytransform = transform;    // caches the value of transform to optimize runtime (sorta)
 }

 // Use this for initialization
 void Start () {
     //finds the player
     GameObject go = GameObject.FindGameObjectWithTag("Player");
     // enemy target is the players  x,y,z position...
     target = go.transform;


 }

 // Update is called once per frame
 void Update () {

     //look at player

     mytransform.rotation = Quaternion.Slerp(mytransform.rotation, Quaternion.LookRotation(target.position- mytransform.position), rotationspeed *Time.deltaTime);

     //finds the distance between player and enemy
     aggroradius =  target.position - mytransform.position;

     // if the positive value of the distance between the player and the enemy is less than  the attack distance - hulk smash    //jk
     if (aggroradius.magnitude < attackdistance)
     {
     // move towards player
     mytransform.position += mytransform.forward  * movespeed * Time.deltaTime;
     }


     // do the funky chicken 
     animation.Play("walk");

     // if (distance = good)
     // attack  ?
     //animation.Play ("attack");

 }

}

thanks !

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 Peter G · Aug 26, 2010 at 11:27 PM

I would not recommend creating a continuous movement by modifying the transform.position because it will clip through objects like the terrain unlike other methods such as CharacterController.Move(). But, to answer your question your character is not directly gravitating to his feet as in being pulled to the position, but is aligning himself to go into them, due to the way you have the character set up. Since the character aims "forward" at the target's feet and only moves forward, he will "gravitate" to the target's feet. The only reason I make that difference is because when I think gravitate I think being pulled in a direction arbitrary to your forward direction. Now, that might not be a good definition, but it was supposed to help clarify what part was causing the problem (the rotation).

The easiest, way to fix this would be to store the target's position in a Vector3 (note: Vector3 is valueType, Thats important otherwise we would move the target also.) Then make the y the same as the player.

i.e.

var aimPoint = target.position; aimPoint.y = transform.position.y;

mytransform.rotation = Quaternion.Slerp(mytransform.rotation, Quaternion.LookRotation(aimPoint), rotationspeed *Time.deltaTime);

Another way that would involve more work would be to set up your character to move using a CharacterController. This would take longer in the short time, but will make movement easier in the long run because you will not have to worry about missed-collisions. You can still do the rotation the same as in your old script the only major difference is moving the player.

var targetPos = target.position - transform.position;
targetPos = targetPos.normalized;
GetComponent(CharacterController).Move(targetPos * Time.deltaTime * moveSpeed);

You can cache a link to the character controller see the performance optimization page.

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 bob 1 · Aug 27, 2010 at 12:22 AM 0
Share

target = go.transform; aimpoint = target.position; aimpoint.y = transform.position.y;

i wanted to go with the top route you provided , and this is where i put the variables, (as comparing to my code above) the monster seems to stay on land without sinking but now seems to walk away from my "hero" ins$$anonymous$$d of walking towards him.. any ideas ? thanks :)

avatar image bob 1 · Aug 27, 2010 at 12:37 AM 0
Share

it works but the monster walks away from the hero ins$$anonymous$$d of towards him (trying to be more clear)

avatar image bob 1 · Aug 27, 2010 at 12:50 AM 0
Share

update

swapped mytransform.rotation = Quaternion.Slerp(mytransform.rotation, Quaternion.LookRotation(target.position- mytransform.position), rotationspeed *Time.deltaTime);

for

mytransform.LookAt(aimpoint); while adding

aimpoint= target.position; aimpoint.y = transform.position.y;

to the update function
:) thanks !

avatar image Peter G · Aug 27, 2010 at 02:05 AM 0
Share

Edit: Glad to here you fixed your problem :) The mistake I made was this: var var aimPoint = target.position - transform.position; aimPoint.y = 0; So glad to here you fixed it, but that's the correction if you want it.

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

No one has followed this question yet.

Related Questions

Follow within a certain distance? 1 Answer

Enemy is not stopping following the player 2 Answers

transporting player to gameobject when it collides with different gameobject 0 Answers

How to stop my ghost AI from teleporting inside a wall 1 Answer

Manipulating Variables Over Multiple Scripts 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