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 /
avatar image
0
Question by 3kWikiGames · Sep 13, 2018 at 04:31 PM · 2dmovement3dscreentoworldpointfollow player

Player wont follow touch controls?

I am trying to make a game for my phone that would let the player follow your finger as its controls (similar to agario). The code I have so far worked with 2D but no longer works when I try to make it a 3D sphere, anyways of fixing this? Thanks! :) (EDIT: The main error I keep on getting is "object reference not set to an instance of an object")?

 using System.Collections;
 using System.Collections.Generic;
 using UnityStandardAssets.CrossPlatformInput;
 using UnityEngine;
 
 public class playerController : MonoBehaviour {
     public float moveSpeed = 0.2f;
     public GameObject player;
     private Rigidbody rb;
     // Use this for initialization
 
     private void Update()
     {
         Vector3 Target = Camera.main.ScreenToWorldPoint(CrossPlatformInputManager.mousePosition);
         Target.z = transform.position.z;
        // player.transform.LookAt(target);
         transform.position += Vector3.MoveTowards(transform.position, Target, (moveSpeed * Time.deltaTime) / transform.localScale.x);
     }
     
 }

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

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Der-Die-Das · Sep 13, 2018 at 05:47 PM

I'm not sure how ScreenToWorldPoint works in 3D since it can't know the depth you want the point to be at.. But I'd solve the problem with a Raycast. You probably have a ground or something where the Sphere is moving on. I'd raycast from the camera onto this ground and use the hit position as Target. I'll show how to raycast further below..

Or you could also just use the delta of the touch position to move the sphere not to a specific position but rather in what direction you want to move it. Like so:

 private void Update()
 {
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
     {
         // Get movement of the finger since last frame
         Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
 
         // Move object across XY plane
         transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
     }
 }

Like that you should be able to move the Sphere based on your delta position. I'm not sure how Agar.io does it but that would be the fastest and easiest way, I think.

But here is the Raycast example:

  RaycastHit hit;
         // Does the ray intersect any objects
         if (Physics.Raycast(transform.position, Camera.main.transform.forward, 
             out hit, Mathf.Infinity))
         {
             //The position of the click on the ground
             Vector3 Target = hit.point;
         }

Be sure that the "ground" has a collider and a Rigidbody for this to work.

You should probably use the delta position method, it's way easier ^^

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 3kWikiGames · Sep 13, 2018 at 06:45 PM 0
Share

I should have known!!! I was looking at other tutorials that explained using ScreenToWorldPoint ins$$anonymous$$d. Luckily I have some $$anonymous$$or experience with raycasting, thanks for the help!

avatar image 3kWikiGames · Sep 13, 2018 at 10:35 PM 0
Share

I've kept trying to get it to work but it still isn't following the persons finger around like I had hoped, and it seems to be having issues whenever it collides with other objects. Will keep working at it until I find something!

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

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

Objects stuck dragging across floor 1 Answer

Standing on player to follow his movement,Follow movement when standing on moving platform/player 1 Answer

ScreenToWorldPoint question 1 Answer

Mouse Click Based Movement/Actions & 2D Sprite Rotation in 3D Space 0 Answers

Move object along non-physical diagonal line 2 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