- Home /
Drag ball left and right
Hi I am a beginner at programming and I am trying to make a ball drag exactly like rolling sky ball,you can help by showing the code,thank you.
Answer by viknesh2020 · Sep 26, 2018 at 12:11 PM
You can consider the following
public float distance = 10f;
void OnMouseDrag(){
Vector 3 mouseposition = new Vector3(Input.mouseposition.x, Input.mousposition.y, distance);
Vector3 ballPosition = Camera.main.ScreenToWorldPoint(mouseposition);
transform.position = ballPosition;
}
Play around with this script to freeze the movement only in one axis or try freezing the ball position in Rigidbody component.
This is working,but i have a problem it's going under the ground too,i tried to freeze the ball position but still the same,i am new to program$$anonymous$$g so thanks.@viknesh2020
@quavo1 Try using this https://assetstore.unity.com/packages/tools/input-management/lean-touch-30111
This has a library of touch functions. Go through their sample scenes for better understanding !
I looked a lot but couldn't find anything relative to my problem,the result of being a beginner @viknesh2020
Answer by aviad59 · Sep 25, 2018 at 11:11 PM
Hey, u could just look for it on google, but anyway here it is:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
Your answer
Follow this Question
Related Questions
touch controls help dragging a charater 0 Answers
How do i change mouse controls to touch controls 0 Answers
Touches on a touch monitor aren't creating Touch objects nor incrementing Input.touchCount 0 Answers
How can my blocks be selected by sliding over them rather than needing to be individually clicked? 0 Answers
Player can jump on enemy and not die 0 Answers