- Home /
Set rotation based on 2 points problem

 #pragma strict
 var isQ : boolean;
 var arrow : GameObject;
 
 function Start () {
 
 }
 
 function Update () {
     if(Input.GetKeyDown(KeyCode.Q)){
         isQ = true;
         arrow.SetActive(true);
     }
     if(isQ){
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         var hit : RaycastHit;
         if(Physics.Raycast(ray,hit)){
             var arrowPos = Vector3(hit.point.x,0,hit.point.z);
             arrow.transform.position = arrowPos;
             var tR = Quaternion.LookRotation(transform.position - arrow.transform.position);
         }
     }
 }
I've no Idea where to start... Can someone help me? :)
               Comment
              
 
               
              If this script is connected to the ball, then you are 99 percent of the way there. I think all you need to do is is add this below line 20::
 arrow.transform.rotation = tR;
In some ways this is a bit backwards from how folks usually solve this problem. Usually they would add the script to the arrow and use Trasnform.LookAt(). Note you may have to adjust your look position so that the 'Y' value matches the arrow.
Thanks for your comment :) however someone on IRC helped me :)
 
               Best Answer 
              
 
              Answer by skoandi · Mar 30, 2013 at 06:29 PM
Someone on #unity3d IRC helped me, here's the code in case someone needs it :)
     public Transform Target;
     public float RotationSpeed;
  
     private Quaternion _lookRotation;
     private Vector3 _direction;
  
 
     void Update()
     {
        //find the vector pointing from our position to the target
        _direction = (Target.position - transform.position).normalized;
  
        //create the rotation we need to be in to look at the target
        _lookRotation = Quaternion.LookRotation(_direction);
  
        //rotate us over time according to speed until we are in the required rotation
        transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
     }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                