Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Thunderball91 · Mar 12, 2014 at 06:15 PM · 2dinstantiatetopdown

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);
         }
     }
 
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image perchik · Mar 12, 2014 at 06:17 PM 0
Share

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?

avatar image Thunderball91 · Mar 12, 2014 at 07:06 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges