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 tayyab43 · Jan 22, 2014 at 10:08 PM · c#rotationfpssoldier

how would i make this work?

Basically i wrote a small bot script and it works but i need the enemy to always point towards the soldier, how would i do this, heres my code:

using UnityEngine; using System.Collections;

public class BOT : MonoBehaviour {

 public GameObject Soldier;
 public GameObject Enemy;
 public float Distance=100f;
 public Rigidbody bulletPrefab;
 public GameObject EnemyBarrelEnd;
 public GameObject gun;
 private Vector3 SoldierPos=new Vector3(0,0,0);
 // Update is called once per frame
 void Update () 
 {
     SoldierPos=Soldier.transform.position;
     Enemy.transform.position=Vector3.Lerp(Enemy.transform.position, Soldier.transform.position,Time.deltaTime);
     animation.Play("soldierRun");
     Distance=Vector3.Distance(Enemy.transform.position,Soldier.transform.position);
     if (Distance<=5)
     {
         animation.Play("soldierFiring");
         Rigidbody rocketInstance;
         rocketInstance = Instantiate(bulletPrefab, EnemyBarrelEnd.transform.position, bulletPrefab.transform.rotation) as Rigidbody;
         rocketInstance.AddForce(Vector3.forward*10000);
     }

 
 
 
 
 }

}

Comment
Add comment · Show 3
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 ikelaiah · Jan 22, 2014 at 10:11 PM 0
Share

Your question's title is way too general. Please consider changing it to something like "How do I set one object to look constantly at another gameObject"? That would be clearer and people can help you better by posing a specific problem.

avatar image FPSworrior · Jan 22, 2014 at 10:15 PM 0
Share

First off it is a specific problem, the person just doesn't know how to do somthing it's not way to general. If your going to comment something tell him how to do what he needs help with and don't be a tool.

avatar image FPSworrior · Jan 22, 2014 at 10:16 PM 0
Share

All you do is use transform.LookAt(Soldier);

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ikelaiah · Jan 22, 2014 at 10:15 PM

Have you look at the documentation on Transform.LookAt()? As per documentation: "It rotates the transform so the forward vector points at target's current position." It seems that this is what you're after.

Here's an example from the Unity's doc -- given in the link above;

     // This complete script can be attached to a camera to make it 
     // continuously point at another object.
     
     // The target variable shows up as a property in the inspector. 
     // Drag another object onto it to make the camera look at it.
     var target : Transform; 
     
     // Rotate the camera every frame so it keeps looking at the target 
     function Update() {
         transform.LookAt(target);
     }


Comment
Add comment · Show 2 · 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 tayyab43 · Jan 23, 2014 at 04:54 PM 0
Share

thanks, i didn't realise it was in the documentation, i better look there first next time :)

avatar image ikelaiah · Jan 23, 2014 at 09:56 PM 0
Share

@tayyab43 That's okay. Life is a lifelong learning. You can help others next time too.

avatar image
0

Answer by komodor · Jan 22, 2014 at 10:12 PM

don't you look for http://docs.unity3d.com/Documentation/ScriptReference/Transform.LookAt.html or http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.RotateTowards.html?

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
avatar image
0

Answer by njpatter · Jan 22, 2014 at 10:43 PM

 Enemy.transform.LookAt(Soldier.transform);

LookAt Documentation

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Rotating camera while wallrunning 1 Answer

Can't stop camera from rotating on Z Axis 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 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