Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Venatal · Nov 16, 2015 at 05:37 AM · c# tutorial

How do you make a clone travel at a fixed velocity in the direction of its rotation?

For example if a clone spawns and has values of (0, 0, 0, 1) it travels to the right at a set speed. How would I script that in C# ? Thanks in advance!

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 ElDo · Nov 16, 2015 at 06:26 AM 0
Share

I'm not sure what your values (0,0,0,1) should mean. But moving objects can be done on several ways depending on how your object is set up some are better than others. The easiest way to explain here I can think of would be to do:

  public float Speed=10f;
     void Update(){
       transform.position+=transform.forward*Speed*Time.deltaTime;
     }

now it moves Forward where Forward is the local Z-Axis of your Object.

avatar image Venatal ElDo · Nov 16, 2015 at 06:38 AM 0
Share

Doesn't seem to work. In my script when a clone spawns its direction is either up, right, down or left. What i want to happen is that when a clone spawns it travels in the direction its facing if you understand what I mean?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by OncaLupe · Nov 16, 2015 at 06:33 AM

It depends on what you're going to do with the object and how (if at all) it interacts with other objects. For simple non-physical movement, you can do something like this:

 using UnityEngine;
 using System.Collections;
 
 public class MoveAlongAxis : MonoBehaviour
 {
     public float moveSpeed = 5f;
     public Vector3 moveDirection = Vector3.forward;
 
     void Update()
     {
         transform.Translate(moveDirection * moveSpeed * Time.deltaTime);
     }
 }

Time.deltaTime is how long the current frame took to render. When multiplied to a speed variable, it adjusts the speed to the framerate. So for instance in the above code, moveSpeed is set to 5, so when multiplied to Time.deltaTime it will make the object move at 5 units per second.

moveDirection is the axis the object will move along. Vector3 contains definitions for each of the axis. 'forward' is the Z axis, 'up' for Y, and 'right' for X. When used in the Translate method, it defaults to using local axis. If you need to move by setting the object's position manually, then you'll need to use transform.forward instead of moveDirection to get the local axis.

 transform.position += transform.forward * moveSpeed * Time.deltaTime;

If you need physics, then the object will have to have a Rigidbody (or Rigidbody2D) attached. Again for simple movement:

 using UnityEngine;
 using System.Collections;
 
 public class MoveAlongAxis : MonoBehaviour
 {
     public float moveSpeed = 5f;
     Rigidbody myRigidbody;
 
     void Start()
     {
         //Store reference to the game object's rigid body. GetComponent is slow.
         myRigidbody = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         myRigidbody.MovePosition(transform.position + (transform.forward * moveSpeed * Time.fixedDeltaTime));
     }
 }

If using 2D system, replace all Rigidbody with Rigidbody2D.

Issue with this is if it gets blocked, it will constantly try to move, which may or may not work depending on how you want your game to run.

With physics you also have the option to just apply a force when it's spawned and let the physics system handle movement from there. You can either use the Start() method, or on the spawner script to trigger this. This is good for things like bullets or other projectiles.

 myRigidbody.AddRelativeForce(moveDirection * moveSpeed, ForceMode.Impulse);

http://docs.unity3d.com/ScriptReference/Transform.Translate.html http://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeForce.html http://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

Comment
Add comment · Show 2 · 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 Venatal · Nov 16, 2015 at 05:49 PM 0
Share

Hmm, dosen't seem to work as I want it. Is there a way to make it move along its rotation? I tried replacing Vector3 with Quaternion but that gives me a error :/

avatar image Venatal Venatal · Nov 16, 2015 at 08:23 PM 0
Share

Got it to work now, thanks a lot dude!

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

35 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 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

Scoring system help 1 Answer

Rocket Launcher Fires Explosion, But Not Rocket 2 Answers

Adding a sound to my Gun Script 0 Answers

Button onclick problem. 0 Answers

I need help fixing this and don't know how.,Hello Unity I am new to it and today I tried to write a script, but when I pressed play it came with the message "All compiler errors have to be fixed 1 Answer


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