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 praniltpof · Jun 13, 2015 at 07:03 PM · unity 5space shooterspaceship

Project space shooter problem

I was watching the projects of unity and i decided to make project space shooter. my space ship, background, lights were ready. Then I make the script to move the space ship as was told in the video. after it was completed, i decided to test it. my space ship was moving but when i moved it, it return to his original position and cannot move my space ship after a certain distance. Help me with this please !!! The script was this :-

using UnityEngine;

using System.Collections;

[System.Serializable]

public class Boundary {

 public float xMin, xMax, zMin, zMax;

}

public class PlayerController : MonoBehaviour {

 public float speed;
 public float tilt;
 public Boundary boundary;
 
 void FixedUpdate ()
 {
     float moveHorizontal = Input.GetAxis ("Horizontal");
     float moveVertical = Input.GetAxis ("Vertical");
     
     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
     GetComponent<Rigidbody>().velocity = movement * speed;
     
     GetComponent<Rigidbody>().position = new Vector3 
         (
             Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax), 
             0.0f, 
             Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
             );
     
     GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);


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 DiegoSLTS · Jun 23, 2015 at 06:25 PM 1
Share

Did you set the speed, tilt and boundary correctly in the inspector? I looked at the original source of the example and your code should work O$$anonymous$$, so maybe you have some components with a different setup.

avatar image Zoinksta · Jun 23, 2015 at 06:47 PM 0
Share

Could you clarify the issue? Is your ship returning to origin after a few moments of movement? I'm learning Unity myself and am currently on this tutorial but I had no issues with this part and our code looks identical.

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by komodor · Jun 23, 2015 at 06:06 PM

if you want to use physics in the game, then you should probably AddForce to the rigidbody (http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html) instead of setting velocity and position of rigidbody

if you want to move the gameobjects yourself then you don't need rigidbody at all and you should affect Transform.position of the gameobject

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
avatar image
1

Answer by tbrosh23 · Jul 01, 2015 at 04:08 PM

I had the same problem! It is because you did not change the boundary values after you wrote them into your code.

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
avatar image
0

Answer by Trys10Studios · Aug 16, 2015 at 08:56 AM

Yes the boundary has to be reset in the scene view, or at least that's what I did. Thank you for the answer!

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
avatar image
0

Answer by Darkskyb2 · Nov 03, 2016 at 06:17 PM

 private Rigidbody rb;

 void Start () 
 {
     rb = GetComponent <Rigidbody> ();
 }

Include it, so you dont have to put GetComponent (); every time you want to call the rigidbody. just need to put this (Ex: rb.position)

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

6 People are following this question.

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

Related Questions

is it always necessary to use an object? 1 Answer

How do Code this? AddScore is outdated. 2 Answers

Unity 5 3D Space Movement Issues 0 Answers

How Can I change between functions after a certain time? 1 Answer

Hello, I am working on a spaceship game and can't get my score to show or pop up. I need help. 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