- Home /
How to Make an Object turn Toward Another
So I have a program, and in it I am trying to have a tracking missile. This isn't working out so well, because, I can only get it to look where its going. I cant get it to turn toward the player...
void Update () { this.rigidbody.AddRelativeForce(0,0,speed); this.transform.rotation = Quaternion.LookRotation(Vector3.forward, Vector3.up); }
Answer by robertbu · Dec 01, 2013 at 03:30 PM
You need access to the player's transform to make this change. The code below assumes you named your player 'Player'. Change the string as appropriate. Also you may have to up the Also you may want to up the 'drag' setting for the rigidbody to decay previous velocity and make the turns target better. I've moved the calculations into FixedUpdate():
using UnityEngine;
using System.Collections;
public class RayCastShoooting : MonoBehaviour {
public float speed = 3.0f;
private Transform player;
void Start() {
player = GameObject.Find("Player").transform;
}
void FixedUpdate () {
transform.LookAt(player);
rigidbody.AddRelativeForce(0,0,speed * Time.fixedDeltaTime);
}
}
Your answer
Follow this Question
Related Questions
Avart Fate with Gun Movement 0 Answers
unlocking weapon (fps) 1 Answer
Multiple Weapon Animation Help 0 Answers
FPS Camera+weapon setup? 0 Answers