Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help making object rotate toward other object (Solved)

Discussion in 'Scripting' started by Retrospark, Aug 11, 2014.

  1. Retrospark

    Retrospark

    Joined:
    Sep 28, 2012
    Posts:
    30
    Code (JavaScript):
    1. #pragma strict
    2. var step = speed * Time.deltaTime;
    3. var from: Transform;
    4. var speed : float;
    5.  
    6.  
    7. function OnMouseEnter () {
    8.     renderer.material.color = Color.white;
    9. }
    10.  
    11.  
    12. function OnMouseExit () {
    13.     renderer.material.color = Color.black;
    14.     Debug.Log ("Mouse Off");
    15. }
    16.  
    17.  
    18. function OnMouseDown(){
    19.         Debug.Log ("MouseButtonDown");
    20.         from.transform.position = Vector3.MoveTowards(this.transform.position, from.position, step);      
    21. }
    22.  
    This code allows me to move object 1 (from) to object 2 (this) , but I can't seem to figure out how to script the rotation. I've looked at a lot of other codes online, but I can't seem to figure out what values to change? Also, how would I go about making the object actually slowly move there like the character controllers movement instead of instantly moving there?
     
  2. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    For the rotation, use transform.LookAt : http://docs.unity3d.com/ScriptReference/Transform.LookAt.html


    Once you're looking at your target you could just move your transform forward by multiplying by speed and Time.deltatime, where speed is a public variable :

    transform.position = transform.position + transform.forward * speed * Time.deltaTime;
     
    Retrospark likes this.
  3. Retrospark

    Retrospark

    Joined:
    Sep 28, 2012
    Posts:
    30
    Awesome, I got it. Thanks.
     
  4. Retrospark

    Retrospark

    Joined:
    Sep 28, 2012
    Posts:
    30
    Ok, so now I have no idea what to do about the speed? How can I make it to where the player doesn't rotate instantly and also (technically) warp to the location?
     
  5. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    You would need two different speed variables. One for the rotation and one for the movement. Given that you have a variable with a rotation speed, you can use Quaternion.Lerp. http://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html

    Code (CSharp):
    1. public class ExampleClass : MonoBehaviour {
    2.     public float rotationspeed = 0.1F;
    3.     public float movementspeed = 0.1F;
    4.     void Update() {
    5.         Vector3 relativePos = target.position - transform.position;
    6.         Quaternion targetrotation = Quaternion.LookRotation(relativePos);
    7.  
    8.         transform.rotation = Quaternion.Lerp(transform.rotation, targetrotation, Time.time * rotationspeed);
    9.  
    10.         transform.position = transform.position + transform.forward * movementspeed * Time.deltaTime;
    11.     }
    12. }
    I haven't tried this code in my editor but it should work, the "target" variable is the transform of your target that you want to follow.
     
    Retrospark likes this.
  6. Retrospark

    Retrospark

    Joined:
    Sep 28, 2012
    Posts:
    30
    Works perfectly. Thanks. I appreciate you writing out code which you didn't have to, but thank you a lot. I'll definitely learn a lot from this.
     
    DoomSamurai likes this.
  7. Retrospark

    Retrospark

    Joined:
    Sep 28, 2012
    Posts:
    30
    Code (CSharp):
    1. Vector3 relativePos = transform.position - transform.position;
    2.         Quaternion targetrotation = Quaternion.LookRotation(relativePos);
    3.        
    4.         target.transform.rotation = Quaternion.Lerp(transform.rotation, targetrotation, Time.time * rotationspeed);
    5.        
    6.         target.transform.position = transform.position + transform.forward * movementspeed * Time.deltaTime;
    Ok, I've added
    Code (CSharp):
    1. public Transform target;
    and specifically speaking, changed the code to
    Code (CSharp):
    1. target.transform.rotation = Quaternion.Lerp(transform.rotation, targetrotation, Time.time * rotationspeed);
    2.        
    3.         target.transform.position = transform.position + transform.forward * movementspeed * Time.deltaTime;
    But when I do this, the player doesn't move slowly to the object anymore, the player moves instantly?

    P.S. Sorry for the continuous question after you've answered the posts title.
     
  8. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    right now you're rotating and moving the target, not your transform. you should write

    transform.rotation = .....

    and

    transform.position =


    Your game object does not move at all? Did you set the target variable by dragging the target transform in your inspector on the script ?
     
  9. Retrospark

    Retrospark

    Joined:
    Sep 28, 2012
    Posts:
    30
    I understand, but the script is kind of backwards from what I am wanting. See, the script moves the object that the script is assigned to toward the targeted object. I need it where the script moves the targeted object towards itself(the object the script it assign to).
     
  10. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    oh! it's the same really. You can just add the script to the target object and set IT'S target as your transform.

    OR, if you want to move the target object from your transform, you can do this :

    Code (CSharp):
    1. void Update() {
    2.         Vector3 relativePos = transform.position - target.position;
    3.         Quaternion targetrotation = Quaternion.LookRotation(relativePos);
    4.         target.transform.rotation = Quaternion.Lerp(target.transform.rotation, targetrotation, Time.time * rotationspeed);
    5.         target.transform.position = target.transform.position + target.transform.forward * movementspeed * Time.deltaTime;
    6.     }
     
    Retrospark likes this.
  11. Retrospark

    Retrospark

    Joined:
    Sep 28, 2012
    Posts:
    30
    Yes! That's what I was trying to do, but couldn't figure out for the life of me! Now the movement is click and move rather than click and continuous move? What could I do to make the movement continuous instead of having to click everytime?

    Edit: Ahh, figured it out. I just put the script you gave me in an Update function and added a boolean for when the object is clicked.
     
    Last edited: Aug 12, 2014
  12. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Time.time * rotationspeed => Time.deltaTime* rotationspeed (for a smooth effect) :)
     
  13. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    lol
    I somehow didn't catch that one :) Copy/paste error