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 Azaroth · Oct 08, 2012 at 09:50 AM · forwardfaceroation

Character Controller Rotation

Hello guys. I made small island and i have 2 first person character controllers. The one is the player and the other is the enemy (i removed the camera from the enemy so when i press play, unity gives me the player). I putted a sphere trigger collider to the enemy so when i get to the "enemy area" the enemy shoots me bullets (small spheres). The problem is that i cant make the enemy face me all the time so bullets come on me, or just for the player to understand that the enemy has perceived him. Its something about rotation and the enemy forward or something like that. Can anyone help me?

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 aldonaletto · Oct 08, 2012 at 10:59 AM

You could calculate the enemy->player direction and Slerp the enemy forward to it - but the calculated direction must be strictly horizontal, or the enemy will tilt weirdly when the heights are different:

 var turnSpeed: float = 5;
 
 private var target: Transform;
 
 function OnTriggerEnter(other: Collider){
   if (other.tag == "Player"){ // player entered trigger:
     target = other.transform; // make it the target
   }
 }
 
 function OnTriggerExit(other: Collider){
   if (other.tag == "Player"){ // player exited trigger:
     target = null; // no target at sight
   }
 }
 
 function Update(){
   if (target){ // if target at sight...
     // find the target direction:
     var dir = target.position - transform.position;
     dir.y = 0; // make direction strictly horizontal
     dir.Normalize(); // normalize it
     // turn gradually to the target each frame:
     transform.forward = Vector3.Slerp(transform.forward, dir, turnSpeed * Time.deltaTime);
     // your attack code may be here:
     // -- attack code --
   }
 }

The code above may be different from what you already have - adapt the direction part to your current code, or adapt your shooting code to the script above.

EDITED: This is the C# version:

 public float turnSpeed = 5;
 
 Transform target;
 
 void OnTriggerEnter(Collider other){
   if (other.tag == "Player"){ // player entered trigger:
     target = other.transform; // make it the target
   }
 }
 
 void OnTriggerExit(Collider other){
   if (other.tag == "Player"){ // player exited trigger:
     target = null; // no target at sight
   }
 }
 
 void Update(){
   if (target){ // if target at sight...
     // find the target direction:
     Vector3 dir = target.position - transform.position;
     dir.y = 0; // make direction strictly horizontal
     dir.Normalize(); // normalize it
     // turn gradually to the target each frame:
     transform.forward = Vector3.Slerp(transform.forward, dir, turnSpeed * Time.deltaTime);
     // your attack code goes here:
     // -- attack code --
   }
 }

Notice that basically the differences are restricted to the way variables and functions are declared.

Comment
Add comment · Show 7 · 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 Azaroth · Oct 08, 2012 at 05:51 PM 0
Share

Hey, thank you for your code, could you give me the C# version?

avatar image Azaroth · Nov 23, 2012 at 08:02 PM 0
Share

thank you :D

avatar image bajskorven · Apr 12, 2013 at 04:50 PM 0
Share

It´s already C# ....

avatar image Azaroth · Apr 12, 2013 at 07:12 PM 0
Share

i wrote it before he add the c# version

avatar image Azaroth · Apr 19, 2013 at 07:45 AM 0
Share

I have a problem with the movement, i have a skeleton with run animation. The animation and the movement to the player works, but without any collider and rigidbody, so he fly. When i add a capsule collider and rigidbody, he falls, like a frozen capsule to the ground. If i add a disk-like collider under his feet he doesnt fall and the animation plays but he cant move. What to do?

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

11 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

Related Questions

Face the direction of movement. 2 Answers

How to update the rigidbody position with respect to ar face movement in ar foundation? 0 Answers

How do you turn object to face a particular vector ? 1 Answer

GameObject facing fowards 1 Answer

controller.simplemove having issues with vector3.forward 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