Question by
opfromthestart · Feb 22, 2017 at 08:31 AM ·
c#transformgame
Character moves without arrow keys
i am making a game with C# and Unity. I cannot find anything, but when I play it, my character starts slowly moving to the left.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ForCamera : MonoBehaviour
{
public float Speed = 1;
public float TurnSpeed = 1;
void Update()
{
Transform astro = GameObject.Find("astro").transform;
Transform Camera = GameObject.Find("Camera").transform;
this.transform.position = new Vector3(astro.position.x, astro.position.y, astro.position.z);
GameObject.Find("astro").GetComponent<Rigidbody>().velocity = new Vector3(Input.GetAxis("Horizontal") * Time.deltaTime * Speed, (Input.GetKey(KeyCode.Space) ? 1f : -1f), Input.GetAxis("Vertical") * Time.deltaTime * Speed);
Camera.rotation = new Quaternion(Camera.rotation.x, Camera.rotation.y, Camera.rotation.z, Camera.rotation.w);
Camera.Rotate(Input.GetAxis("Mouse Y") * Time.deltaTime * -TurnSpeed * Speed, Input.GetAxis("Mouse X") * Time.deltaTime * TurnSpeed * Speed, 0f);
//GameObject.Find("astro").transform.Rotate(0f, Input.GetAxis("Mouse X") * Time.deltaTime * TurnSpeed * Speed, 0f);
var tempVar = Camera.eulerAngles;
tempVar.z = 0f;
Camera.eulerAngles = tempVar;
var tempVar2 = Camera.eulerAngles;
tempVar2.x = 0;
astro.eulerAngles = tempVar2;
print(Input.GetAxis("Horizontal")+" "+Input.GetAxis("Vertical"));
//print(GameObject.Find("Camera").transform.rotation);
if (Input.GetKeyDown(KeyCode.E))
{
astro.position = new Vector3(0f, 2f, -6f);
astro.rotation = new Quaternion();
Camera.rotation = new Quaternion();
GameObject.Find("astro").GetComponent<Rigidbody>().velocity = Vector3.zero;
print("ok");
}
if (Input.GetKey(KeyCode.Space))
{
GameObject.Find("astro").GetComponent<Rigidbody>().velocity += Vector3.up*.3f;
print("runs");
}
}
public Texture2D crosshairImage;
private void Start()
{
Cursor.visible = false;
}
private void OnGUI()
{
Cursor.visible = false;
float xMin = (Screen.width / 2) - (crosshairImage.width / 2);
float yMin = (Screen.height / 2) - (crosshairImage.height / 2);
GUI.DrawTexture(new Rect(xMin, yMin, crosshairImage.width, crosshairImage.height), crosshairImage);
}
}
Any answers will be appreciated.
Comment
Your answer
Follow this Question
Related Questions
Make object move back and forth 2 Answers
Vector3.Lerp not working properly, making the player bounce around 2 Answers
Vector3.MoveTowards moving transform 0 Answers
Is there a transform method equivalent to assigning euler angle offsets? 1 Answer
I don't know how to create a highscore gui text and display it at death screen 0 Answers