The question is answered, right answer was accepted
Using the roll a ball tutorial my dart backs off the canvas
Hi, I am using the movement script from the roll a ball tutorial, but as soon as I start the game my dart backs off the canvas all by itself.
Here's the code I'm using;
using UnityEngine;
using System.Collections;
public class PlayerMovement : 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);
}
}
Also, I want to use the arrow keys to move the dart left and right only, where would I need to put that script? In a new C# script or somewhere in this one possibly?
Any help would be totally appreciated :) Thanks
Answer by Darth_Cheree · Sep 12, 2015 at 02:33 AM
This is my latest attempt to keep my dart only moving left and right.
using UnityEngine; using System.Collections;
public class moveDart_LR : MonoBehaviour {
float speed = 1.0f;
void Update() {
if (Input.GetKey (KeyCode.RightArrow))
transform.Translate(Vector2.right * Time.deltaTime);
if (Input.GetKey (KeyCode.LeftArrow))
Transform.Translate(Vector2.left * Time.deltaTime);
}
}
Ok so I am answering my own question here as I just found the answer. I took the sprite off of Interpolate and used this code to make the sprite move left and right using the arrow keys.
using UnityEngine;
using System.Collections;
public class moveDart_LR : $$anonymous$$onoBehaviour {
public float speed = 1.0f;
void Start(){
}
void Update(){
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow))
transform.position += new Vector3 (speed * Time.deltaTime,0.0f,0.0f);
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow))
transform.position -= new Vector3 (speed * Time.deltaTime,0.0f, 0.0f);
}
}
Follow this Question
Related Questions
Why is this not working? 1 Answer
Enemy destroys player if scale is bigger 1 Answer
Saving data in a txt file from Input Field button? 0 Answers
A namespace can only contain types and namespace declarations..(39,14) 0 Answers
unity c# money system 2 Answers