Quantcast

Jump to content

» «
Photo

Making a Game

2 replies to this topic
BAF-stueycow
  • BAF-stueycow

    The Car Place Founder

  • Members
  • Joined: 21 Sep 2012
  • England

#1

Posted 21 March 2013 - 05:26 PM

Hi. I am making a game in unity.

It is a simple driving game and i intend to make it a PC download and a Android game.

I am making the car etc.. but need help

I am writing the code (C#)

so far i can make the car go forward. but can not make it turn

code so far...

CODE
using UnityEngine;
using System.Collections;

public class Car: MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
 if(Input.GetKey(KeyCode.W)) {
  transform.position += transform.forward * 5.0f * Time.deltaTime;
 
 }
}
}


if you can help message me I am currently working on a website so... Hope you can help

Rawra
  • Rawra

    Daedric Prince of Madness

  • Members
  • Joined: 06 Aug 2007

#2

Posted 21 March 2013 - 06:46 PM Edited by Rawra, 21 March 2013 - 07:32 PM.

Hi

I don't know how much help I will be as I don't use C#, but what is transform.position? A vector? If so, you would want to manipulate the individual elements (x,y ) separately such as :
CODE

if(Input.GetKey(KeyCode.W)) {
 transform.position.y += transform.forward * 5.0f * Time.deltaTime;
if(Input.GetKey(KeyCode.A)) {
 transform.position.x += transform.right* 5.0f * Time.deltaTime;

or something along those lines


Edit:

Sorry, I thought you wanted it to MOVE right, not rotate

BAF-stueycow
  • BAF-stueycow

    The Car Place Founder

  • Members
  • Joined: 21 Sep 2012
  • England

#3

Posted 21 March 2013 - 07:29 PM

QUOTE (Rawra @ Thursday, Mar 21 2013, 18:46)
Hi

I don't know how much help I will be as I don't use C#, but what is transform.position? A vector? If so, you would want to manipulate the individual elements (x,y ) separately such as :
CODE

if(Input.GetKey(KeyCode.W)) {
 transform.position.y += transform.forward * 5.0f * Time.deltaTime;
if(Input.GetKey(KeyCode.A)) {
 transform.position.x += transform.right* 5.0f * Time.deltaTime;

or something along those lines

Right. UPDATE

Working script
CODE
using UnityEngine;
using System.Collections;

public class Car : MonoBehaviour {
public float forwardSpeed = 5.0f;
public float backwardSpeed = -2.0f;
public float turnRate = 80.0f;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
 if(Input.GetKey (KeyCode.S)){
  transform.position += transform.forward * backwardSpeed * Time.deltaTime;
 }
 if(Input.GetKey (KeyCode.W)){
  transform.position -= transform.forward * forwardSpeed * Time.deltaTime;
 }
 if(Input.GetKey (KeyCode.A)){
  transform.Rotate (0.0f, -turnRate * Time.deltaTime, 0.0f);      
 }
 if(Input.GetKey (KeyCode.D)){
  transform.Rotate (0.0f, turnRate * Time.deltaTime, 0.0f);
 }
}
}


That is the script for moving the car.

A little guide.

Transform.position if for the movement of the car mainly for things like a auto programed thing staying a certan distance from a tree.

public float is so you can edit the variables.

And thanks for the help Rawra




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users