- Home /
Instantiated object won't move!
Hi all,
I am trying to get an instantiated object (prefab) to travel in the direction its facing, however when it instantiates the object it doesn't move. I cannot figure out why. My code is below, thanks in advance:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
// Movement Speed and Force applied when hit!
public float moveForce = 365f; // Amount of force added to move the player in all directions.
public float maxSpeed = 5f; // The fastest the player can travel in the both axis.
public Rigidbody2D Arrow;
public float projSpeed = 50f;
// Arrows
public int arrowAmmo = 20;
public bool CanFire = false;
// Rotation Variables
private Vector3 mousePos;
private Vector3 lookPos;
private float mouseX;
private float mouseY;
public float angle;
void Awake()
{
//anim = GetComponent<Animator>();
}
void Update()
{
InputControl();
RotationControl();
}
void FixedUpdate ()
{
}
void RotationControl ()
{
// Updates Mouse Co-ordinates
mousePos = Input.mousePosition;
// Set out Mouse Position.z variable to 0 as it is not needed.
mousePos.z = 0;
// Using lookPos we allow the object to transoform its position.
lookPos = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - transform.position;
// Setting up the angle using both the y and x co-ords multiplied by 57.2... stored as Rad2Deg by Unity
angle = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
// Finally Rotate the object by using a quaternion angle axis which uses the angle variable as well as the vector3.forward
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
void InputControl()
{
// later on check the game state and make sure we are in game playing and not paused :)
// Left
if(Input.GetKey ("a"))
{
// Set movement speed
transform.position -= Vector3.right * maxSpeed * Time.deltaTime;
}
if(Input.GetKey ("d"))
{
// Set Movement speed using the max speed and delta timer
transform.position += Vector3.right * maxSpeed * Time.deltaTime;
}
if(Input.GetKey("w"))
{
transform.position += Vector3.up * maxSpeed * Time.deltaTime;
}
if(Input.GetKey("s"))
{
transform.position -= Vector3.up * maxSpeed * Time.deltaTime;
}
if(Input.GetMouseButton(1) && arrowAmmo > 0)
{
Rigidbody2D arrowInstance = Instantiate(Arrow, transform.position, transform.rotation) as Rigidbody2D;
arrowInstance.AddForce(transform.forward * 6);
}
}
}
Have you tried adding Debug.Log statements to see if it's actually getting into the rotationcontrol function, and to see what the angle/axis is?
It is getting access to the rotation and is rotating fine, it's just refusing to travel anywhere. I've checked to make sure that the prefab is not kinematic.
Answer by Thunderball91 · Mar 13, 2014 at 01:57 PM
Fixed it after looking up the Unity2D documentation, there is no transform.forward in the Rigidbody2D type so I changed it to transform.right projSpeed Time.DeltaTime and they are moving! Thanks for the help anyway perchik :) I'll be in the corner facepalming for not trying the .right/.left options.
Your answer
Follow this Question
Related Questions
How in the world do I ACCURATELY place an object at the mouse? 3 Answers
Instantiate after setting values? 0 Answers
How to instantiate a gameobject by aligning it to another one 2 Answers
A null value was found where an object instance was required. 1 Answer
Top down 2d shooting - XZ rotation 0 Answers