• 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 akirakawai · Apr 21, 2014 at 07:29 PM · c#gameobjectmovement

How to make players follow only the movement but not the position / vector.

First of all, sorry for my English.

I am now doing 2d top down game. I want when I move my player to the up/down or left/ right, another gameObject will follow even it is not in a same position / vector. At the same time it was in the mirror movement. I mean when I move right, it will move to the left. But if I get stuck on some collider but still pressing forward button, that gameObject will still moving forward if there is no collider in front of him.

Here is the code I use and work perfectly in mirror effect movement. But I cannot achieve what I want using this code. I want to know:

1) How can I make the gameObject still moving forward (no collider in front of him) when I pressed forward button even if I stuck on some collider.

2) How can I stop the gameobject from going through the wall?

     using UnityEngine;
     using System.Collections;
      
     public class Mirror : MonoBehaviour {
      
     public GameObject mirrorObject; // Object to mirror
     public bool mirrorX; // Wether or not to mirror the X axis
     public bool mirrorY; // Wether or not to mirror the Y axis
     public bool mirrorZ; // Wether or not to mirror the Z axis
      
     public bool useLateUpdate; // Wether or not to set the object's position in LateUpdate
      
     void Update()
     {
     Vector3 newPosition;
     newPosition = mirrorObject.transform.position; // Get the mirrorObject's position
      
     if(mirrorX)
     {
     newPosition.x = -newPosition.x; // Mirror on the X axis
     }
     if(mirrorY)
     {
     newPosition.y = -newPosition.y; // Mirror on the Y axis
     }
     if(mirrorZ)
     {
     newPosition.y = -newPosition.y; // Mirror on the Z axis
     }
      
     if(!useLateUpdate)
     transform.position = newPosition; // Set object's position equal to the new mirrored position
     }
      
     void LateUpdate()
     {
     if(useLateUpdate)
     transform.position = newPosition; // Set object's position equal to the new mirrored position
     }
     }
Comment
Add comment · Show 1
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 aceX · Apr 21, 2014 at 07:56 PM 0
Share

Not sure if I understand correctly but, maybe you should try movement using AddForce() and a max speed variable, along with high friction physics materials to avoid any inertia

Something like:

 public float moveForce;
 
 ...
 
 float hInput = Input.GetAxis ("Horizontal");
 
 //If the player is changing directions or hasn't reached the maximum velocity
 if (hInput * rigidbody2D.velocity.x < maxVelocity)
 //...add force to the player in the "hInput" direction
 rigidbody2D.AddForce (Vector2.right * hInput * moveForce);


The same through Input.GetAxis ("Vertical"); and Vector2.up for vertical movement

Then applying the script to the two objects you want to move.

$$anonymous$$ind of like controlling two players at the same time, ins$$anonymous$$d of mirroring their movements.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Making a bubble level (not a game but work tool) 1 Answer

Coroutine fires once after destroying gameobjects in a scene and recreating new objects 0 Answers

Modifying the Transform of a GameObject 1 Answer

Moving a Game Object to the mouse 4 Answers

Gameobjects move towards a moving object 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