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 /
This question was closed Aug 31, 2015 at 09:15 PM by Ashky for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ashky · Oct 02, 2014 at 04:00 PM · c#transform3dtranslatesmoothdamp

How to smooth out movement in a 3D space?

Hello Unity Community!

I am currently working on a 3D space combat game (chicken invaders styled). I've been working on my ship controller, using code like transform.Translate(-Time.deltaTime*speed,0,0); to move my object.

However, it seemed very jerky, so I tried using Vector3.SmoothDamp() instead. Here's how the code looks

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Collections.Generic;
 
 public class ShipController : MonoBehaviour {
     public int speed = 20;
     public float smoothTime ;
     private Vector3 velocity = Vector3.zero;
     Vector3 targetPosition;
     
     private void Start()
     {
         smoothTime = (100/speed);
         targetPosition = new Vector3(-1000,0,0);
     }
     
     void Update() {
 
         //CLAMPING
         Vector3 pos = Camera.main.WorldToViewportPoint (transform.position);
         pos.x = Mathf.Clamp(pos.x, 0.09f, 0.91f);
         pos.y = Mathf.Clamp(pos.y, 0.15f, 0.9f);
         transform.position = Camera.main.ViewportToWorldPoint(pos);
 
         if(Input.GetKey(KeyCode.A))
         {
             transform.position = Vector3.SmoothDamp(this.transform.position, targetPosition, ref velocity, smoothTime);
         }
     }
 }


It works well, smooth, responsive and flexible. It has only one problem: since I'm using targetPosition = new Vector3(-1000,0,0); , my object translates to the left, but also towards the center. I want my object to move only to the left, and not hover towards the center, regardless of its current position.

Or, if someone could suggest and exemplify a different method of implementing this movement, it would be most welcomed.

Thank you for your time!

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by NoseKills · Oct 03, 2014 at 11:39 AM

Your targetPosition has a y-coordinate of 0. I assume that (0,0,0) is in the center of the screen.

Try something like this.

  private void Start()
     {
         smoothTime = (100/speed);
         targetPosition = this.transform.position;
         targetPosition.x = -1000;
     }

This way you set the y and z coordinates to their old values and only change the x.

Comment
Add comment · Show 1 · 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 Ashky · Oct 03, 2014 at 12:37 PM 0
Share

I thought it would be something like this, thank you. Works great!

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Limit & Scaling Translation Rate. 0 Answers

stop object(door) translation 0 Answers

a better movement code C# 3 Answers

C# Smoothing Out transform.Translate 4 Answers

Help with casting a raycast C# 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