Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by M4koren · Mar 13, 2017 at 11:06 PM · rotationmovement

How to make the player object lock on to another object?

I've been inspired by Breath of the Wild to make my game have a similar "lock on" feature. I understand that I need to try and make the player object face the object I'm locked on to, but I have no idea how to write the code for it.

I also want to disable the rotation I already have in the code when I'm locked on, since it will probably mess up the lock on rotation.

Here's my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player1Controller : MonoBehaviour {
 
     public float moveSpeed;
 
     void Start ()
     {
         moveSpeed = 10f;
     }
 
     void Update ()
     {
         float moveHorizontal = Input.GetAxisRaw ("Horizontal");
         float moveVertical = Input.GetAxisRaw ("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
 
         transform.Translate (movement * moveSpeed * Time.deltaTime, Space.World);
     }
 }

My player object is a simple rectangle. I've frozen the X and Z rotations in the rigidbody component.

Also I have another little question that I'm guessing has a quick fix. When I move my player object around, it faces the direction I want it to move, but the direction snaps back to 0 when it stops moving. How do I fix this?

Thanks for your time.

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
Best Answer

Answer by Robert_s_a · Mar 14, 2017 at 04:20 AM

@m4koren

This might work for you.

There's a transform.LookAt(Vector3) method, which will basically make the object look at a specific point in space, which could be the enemy's location.

I'd implement it like this in your Update function:

 if (lockedOn) // However you are going to control and check this
 {
     // This will make the forward (z-axis) of your object point at the enemy
     transform.LookAt(enemy.transform.position);
 }
 else
 {
     // Whatever code you use to control your object's rotation otherwise
 }

And I think probably the problem with your code above is that your rotation is dependent on the movement vector, so when you stop moving, your movement vector is Vector3(0.0f, 0.0f, 0.0f) and your object will Slerp its way over to there. You can probably put a bit of logic around the transform.rotation line, to only apply when you are actually moving.

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 M4koren · Mar 14, 2017 at 05:46 AM 0
Share

This worked like a charm, thanks.

I also needed to add public GameObject enemy; at the top of the script so I could use the enemy as a reference.

avatar image Robert_s_a M4koren · Mar 14, 2017 at 02:39 PM 0
Share

You're welcome.

Yes, true. Good catch. :) I just put the specific bits of code necessary for the motion you were looking for.

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

126 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 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 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 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 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 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

How to return original rotation of object 0 Answers

Record and replay movement 0 Answers

Look Left or right but keep walking forward 0 Answers

How to not execute certain line of code. 2 Answers

moving an object continuously between waypoints 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