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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Making controllable missiles?

Discussion in 'Scripting' started by mineralman, Nov 11, 2015.

  1. mineralman

    mineralman

    Joined:
    Oct 8, 2015
    Posts:
    2
    I'm making a game where the object is to run a missile into various objects. You can control the missile on its journey to the target, which is where the issue is. Ideally, you'd be able to hit W to move up, X to move down, etc. I've been trying a ton of different scripts and nothing really seems to be doing the trick. Does anyone have any advice?

    Here's my current, non-working code (which I took from another post here on the forums and modified):

    using UnityEngine;
    using System.Collections;

    [RequireComponent(typeof(Rigidbody))]
    public class PlayerMovement : MonoBehaviour
    {

    public float movementSpeed = 5.0f;
    public float clockwise = 5.0f;
    public float counterClockwise = -5.0f;

    void Start()
    {

    }

    void Update()
    {
    if (Input.GetKey(KeyCode.W))
    {
    transform.Rotate(0, Time.deltaTime * clockwise, 0);
    }
    else if (Input.GetKey(KeyCode.S))
    {
    transform.Rotate(0, Time.deltaTime * counterClockwise, 0);
    }
    else if (Input.GetKey(KeyCode.A))
    {
    transform.Rotate(Time.deltaTime * clockwise, 0, 0);
    }
    else if (Input.GetKey(KeyCode.D))
    {
    transform.Rotate(Time.deltaTime * counterClockwise, 0, 0);
    }

    else if (Input.GetKey(KeyCode.E))
    {
    transform.Rotate(0, 0,Time.deltaTime * clockwise);
    }
    else if (Input.GetKey(KeyCode.Q))
    {
    transform.Rotate(0, 0, Time.deltaTime * counterClockwise);
    }
    }
    }

    Also, I keep getting these warnings that various scripts are either never used or never assigned to and will always have a default value of zero. I'm not using these scripts, so are those warnings important? If not, how do I get rid of them so I can see the important stuff?

    (As you might've been able to tell, I'm not exactly a Unity vet, so please take things slowly.)

    Thanks!
     
  2. JakeBilbe

    JakeBilbe

    Joined:
    Jun 10, 2015
    Posts:
    57
    This code should work, but mess around with it and even create your own from it to learn how it works.

    Instead of modifying each key to do a certain thing, look at using Vector3s as they're better for this. Use a Character Controller (Remember to modify the capsule to match the missile basic shape) and even look at using a RigidBody if you want to use Physics and more.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent (typeof(CharacterController))]
    6. public class MissileMovement : MonoBehaviour {
    7.    
    8.     public float Speed;
    9.     private Vector3 Dir;
    10.     private CharacterController cc;
    11.  
    12.     private void Start() {
    13.         cc = GetComponent<CharacterController> ();
    14.     }
    15.  
    16.     private void Update() {
    17.         transform.Translate (Vector3.forward * Speed * Time.deltaTime);
    18.         Dir = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
    19.         cc.Move (Dir * Time.deltaTime);
    20.     }
    21. }
    22.  
    23.  
    REQUIREMENTS:
    • Create a script called MissileMovement and paste this in
    • Attach your Camera as a child object to the Missile
    • Attach a Character Controller to Missile
    • Enjoy!
     
    mineralman likes this.
  3. mineralman

    mineralman

    Joined:
    Oct 8, 2015
    Posts:
    2
    Thank you so much! This is really helpful.